//create an empty console.log function if firebug is not running to avoid errors.
if(typeof(window.console) == "undefined"){
	window.console = { log:function(){}};
};

WEBXTRACT = {
	
	content_profile_example_urls:{
		'bbc_news':[
						"http://www.bbc.co.uk/news/technology-13639875"
					],
		'bbc_news_section':[
						"http://www.bbc.co.uk/news/science_and_environment/"
					],
		'guardian_news':[
						"http://www.guardian.co.uk/world/2011/jun/05/yemeni-president-saudi-arabia"
					],
		'youtube_video_page':[
						"http://www.youtube.com/watch?v=q2EaN8aLhLY"
					],
		'blogger_blog':[
						"http://blog.tomcarnell.com/"
					],
		'amazon_product_page':[
						"http://www.amazon.com/Crowd-Non-US-Format-PAL-Region/dp/B000X843UC"
					],
		'11870_page':[
						"http://11870.com/pro/templo-gato"
					],
		'yelp_page':[
						"http://www.yelp.es/biz/la-tita-rivera-madrid"
					]
	}
};

$( document).ready( function(){
	
	$('.img-carousel').each( function() {
		WEBXTRACT.imageCarousel_wireUp( $( this) );
	});
	
	$("a.image").lightBox();
	
	
	$('#content-profile-selector').change( function() {
		
		if( '' == $( this).val() ) {
			return false;
		}
		
		var firstProfileExampleURL = WEBXTRACT.content_profile_example_urls[ $( this).val()][0];
		$('#content-profile-url-input').val( firstProfileExampleURL);
		
		return false;
	});
	
	if (1 === $('#json-textarea').size()) {
		var jsonData = $('#json-textarea').val();
		var jsonDataObject = JSON.parse(jsonData);
		var itemJSONString = JSON.stringify(jsonDataObject, null, 4);
		$('#json-textarea').val(itemJSONString);
	}
	
});


WEBXTRACT.imageCarousel_wireUp = function( carousel) {
	
	var carouselListMask = carousel.find('.img-carousel-mask');
	var carouselList = carousel.find('.img-carousel-mask ul');
	var leftButton = carousel.find( '.slide-left-button');
	var rightButton = carousel.find( '.slide-right-button');
	
	var itemWidth = carouselList.children('li:first').outerWidth( true);
	var itemWidth = 80;
	var numberOfItems = carouselList.children('li').size();
	if( numberOfItems > 3) {
		carousel.find('.slider-controls').slideDown( function() {
			
			//wire up slide buttons:
			leftButton.click( function() {
				if( carousel.hasClass('sliding') ) {return false;} else { carousel.addClass('sliding') }
				
				carouselList.animate({
					left:'+=' + itemWidth
				}, 400, 'swing', function() {
					carousel.removeClass('sliding');
					WEBXTRACT.checkCarouselButtons( carouselList, carouselListMask, leftButton, rightButton);
				});
				return false;
			})
			
			rightButton.click( function() {
				if( carousel.hasClass('sliding') ) {return false;} else { carousel.addClass('sliding') }
				
				carouselList.animate({
					left:'-=' + itemWidth
				}, 400, 'swing', function() {
					carousel.removeClass('sliding');
					WEBXTRACT.checkCarouselButtons( carouselList, carouselListMask, leftButton, rightButton);
				});
				return false;
			})
		});
	}
	var carouselWidth = itemWidth * numberOfItems;
	carouselList.width( carouselWidth).show();
	WEBXTRACT.checkCarouselButtons( carouselList, carouselListMask, leftButton, rightButton);
};

WEBXTRACT.checkCarouselButtons = function( list, listMask, leftButton, rightButton) {
	
	var leftShift = list.position().left;
	
	if( leftShift >= 0) {
		//disabled the left button:
		leftButton.addClass('disabled');
	} else {
		leftButton.removeClass('disabled');
	}
	
	var totalWidth =  listMask.children('ul').innerWidth();
	var displayWidth = listMask.outerWidth( true);
	
	if( Math.abs( leftShift) >= (totalWidth - displayWidth) ) {
		rightButton.addClass('disabled');
	} else {
		rightButton.removeClass('disabled');
	}
	
};
