$(document).ready(function() {
	$('#collect-list, #manage-list').hide();
	
	var imgHrefInit = $('#share-a').attr('rel');
	$('#share-a').attr('href', imgHrefInit).colorbox();
	var imgTitleInit = $('#share-a').attr('title');
	$('#image-panels-magnify a').attr({href: imgHrefInit, title: imgTitleInit}).colorbox();
	
	$('a[href="#share"], a[href="#collect"], a[href="#manage"]').click(imageSlider);
	
	$("label.inlined + .input-text").each(function (type) {
	 	$(this).focus(function () {
	  		$(this).prev("label.inlined").addClass("focus");
	 	});
	 	$(this).keypress(function () {
	  		$(this).prev("label.inlined").addClass("has-text").removeClass("focus");
	 	});
	 	$(this).blur(function () {
	  		if($(this).val() == "") {
	  			$(this).prev("label.inlined").removeClass("has-text").removeClass("focus");
	  		}
	 	});
	});
	
	function imageSlider() {
		var target = $(this).attr('href');
		//exit
	    if ($(target+'-btn').attr('class') === 'current') {
			return false;
		}
		var images = $('#image-panels div').find('img');
		var btns = {
	    	'#share': 1,
	    	'#collect': 0,
	    	'#manage': 2
	    };//starting positions
	    //exit
		if ($(images[btns[target]]).parent().attr('class') === 'cboxElement') {
			return false;
		}
		var buttons = $('#billboard-nav a');
		var speed = 400;
		var styles = [
	        {width: 273, height: 190, top: 30, left: 0},
	        {width: 348, height: 242, top: 0, left: 100},
	        {width: 273, height: 190, top: 30, left: 267}
	    ];
	    
		//buttons
		$(buttons).removeClass('current');
		$(target+'-btn').addClass('current');
		
		//animation
		$(images[btns[target]]).css('left') == '0px' ? slide('right') : slide('left');
		function slide(direction) {
			var right = direction.toLowerCase() === 'right';
			
			if (right) {
				var leftarray = new Array('0px','100px','267px');
				var zarray = new Array(100,90,90);
				var style_indexes = new Array(1,2,0);
			} else {
				var leftarray = new Array('0px','100px','267px');
				var zarray = new Array(90,90,100);
				var style_indexes = new Array(2,0,1);
			}
			$(images).each(function(i){
				switch( $(images[i]).css('left') ) {
					case leftarray[0]:
						var z = zarray[0];
						var s = style_indexes[0];
						break;
					case leftarray[1]:
						var z = zarray[1];
						var s = style_indexes[1];
						break;
					case leftarray[2]:
						var z = zarray[2];
						var s = style_indexes[2];
						break;
				}
				$(images[i]).animate(styles[s], speed, 'easeInOutCubic');
				$('#image-panels-magnify').fadeOut(400, function(){$(this).fadeIn(400)});//magnify image
				setTimeout(function(){$(images[i]).css({zIndex:z});}, speed / 2);
			});
		}//function slide(direction) {
		
		//lists
		$('#collect-list, #share-list, #manage-list').fadeOut(500);
		$(target+'-list').fadeIn(1000);
		
		//unbind click
		$('a[href="#share"], a[href="#collect"], a[href="#manage"]').unbind('click',imageSlider);
		
		//image href
		$(images).each(function(i){
			var relStr = $(images[i]).parent().attr('id');
			var newrelStr = relStr.split('-');
			$(images[i]).parent().attr('href', '#'+newrelStr[0]).removeClass('cboxElement').removeData('colorbox');
		});
		var imgHref = $(images[btns[target]]).parent().attr('rel');
		$(images[btns[target]]).parent().attr('href', imgHref);
		//magnifying glass img
		$('#image-panels-magnify a').removeClass('cboxElement').removeData('colorbox');
		var imgTitle = $(images[btns[target]]).parent().attr('title');
		$('#image-panels-magnify a').attr({href: imgHref, title: imgTitle});
		
		//re-set click
		$('a[href="#share"], a[href="#collect"], a[href="#manage"]').filter(function(){
			return ('#'+$(this).attr('id') != target+'-a');
		}).click(imageSlider);
		
		//colorbox
		$(target+'-a').colorbox();
		$('#image-panels-magnify a').colorbox();
		
		return false;
	}
});
