$(document).ready(function(){
	analyzeURI();	
	
	$('#style-nav li a').click(function(){
	
	   	$('#style-nav li.active').removeClass('active');	
	   	$(this).parent().addClass('active');
	   	var item 	= $('#style-nav a').index(this)+1;
		var left = -959*(item-1);								
		
		$('.slider').animate({left:left+'px'}, 500);
		
		var pos = 54+(24*item)+'px';
		$(this).parent().parent().animate({backgroundPosition:pos+' center'}, 500);		
		
		return false;
	});
	$('.style-group:last').addClass('last');
	
});

function analyzeURI(){
	if(window.location.hash){
		var path = window.location.hash.toString().replace('#', '');
		var map = {
			'a' : 1,
			'b' : 2,
			'c' : 3
		};
		var item = map[path];
		var left = -959*(item-1);	
		$('.slider').animate({left:left+'px'}, 500);		
		$('#style-nav li.active').removeClass('active');
		var pos = 54+(24*item)+'px';
		$('#style-nav ul').animate({backgroundPosition:pos+' center'}, 500);
		$('#style-nav a.option'+item).parent().addClass('active');
	}
}

var Lightbox = {
	
	init:function(){		
		Lightbox.first = $('#styles .style:first').attr('id');	
		Lightbox.last =  $('#styles .style:last').attr('id');
		Lightbox.showBox();
	},
	
	showBox:function(){
		$('.slider span').click(function(){
			var id = $(this).parent().parent().attr('id');
			var pos = parseInt($(this).css('left'));
			switch(pos){
				case 0: 	var item = 0; break;
				case 189:	var item = 1; break;
				case 378:	var item = 2; break;
			}
			
			$.get('/_ajax/styles.php', { id:id, style:item, first:Lightbox.first, last:Lightbox.last, controls:true }, function(o){
				$('body').append('<iframe id="hide_select"></iframe><div id="overlay"></div><div id="lightbox">'+o+'</div>');
				overlayPosition();			
				$(window).scroll(overlayPosition);
				overlaySize();
				Lightbox.slider(item);
			});
			return false;		
		});
	},
	
	slider:function(item){
		Lightbox.current = item;
		var left = -(item*882)+'px';
		$('#inner-slider').animate({ left:left }, 500);
		
		$('#controlbar .schemes a').click(function(){
			var num = $(this).attr('className').split('option')[1];
			$('#inner-slider').animate({ left:-( (num-1)*882)+'px' }, 500);
			
			var bg = 159 + ((num-1)*30);
			$('#controlbar .schemes').animate({backgroundPosition:bg+'px' }, 500);
			
			switch(num){
				case '1': var stilo = 'a'; break;
				case '2': var stilo = 'b'; break;
				case '3': var stilo = 'c'; break;
			}
					
			$('#controlbar .style').html( $('#controlbar .style').html().substr(0,2)+'-'+stilo);
			return false;
		});
		Lightbox.actions();
	},
	
	actions:function(){
		$('#nextprev a').bind('click', Lightbox.nextPrev);		
		$('#close-btn').bind('click', Lightbox.closeIt);
	},
	
	nextPrev:function(){
		var id = $(this).attr('href').replace('#', '');
		
		// Slide up
		$('#inner-slider').slideToggle('slow');
		
		// Update style number and move current locator
		$('#controlbar .style').html(id+'-a');
		$('#controlbar .schemes').animate({ backgroundPosition:'159px'}, 500);
		
		if(id > Lightbox.last) {
			Lightbox.next = parseInt(id)-1;
			$('#next').fadeIn('slow');
			$('#next').attr('href', '#'+Lightbox.next);			
		} else {
			$('#next').fadeOut('slow');
		}
		
		if(id < Lightbox.first) {
			$('#prev').fadeIn('slow');
			Lightbox.prev = parseInt(id)+1;
			$('#prev').attr('href', '#'+Lightbox.prev);
		} else {
			$('#prev').fadeOut('slow');
		}
		
		// Do the ajax call
		$.get('/_ajax/styles.php', { id:id, style:0, first:Lightbox.first, last:Lightbox.last }, function(o){
			$('#inner-slider').html(o).slideToggle('slow');			
			Lightbox.actions();
		});
		return false;
	},
	
	closeIt:function(){
		$('#overlay').remove();
		$('#lightbox').remove();
		$('#hide_select').remove();
		return false;	
	}
}

$(document).ready(function(){
	Lightbox.init();
});

function overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	$("#overlay").css({"height": yScroll, "width": xScroll});
	$("#hide_select").css({"height": yScroll,"width": xScroll});
}

//helper functions below

function TB_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function removeOverlay() {
 	//$("#TB_imageOff").unbind("click");
	$("#overlay").unbind("click");
	//$("#TB_closeWindowButton").unbind("click");
	$("#overlay").fadeOut("fast",function(){$('#lightbox,#overlay,#hide_select').remove();});
	//$("#TB_load").remove();
	return false;
}

function overlayPosition() {
	var pagesize = getPageSize();
	var arrayPageScroll = getPageScrollTop();
	var style = {width: '882px', left: (arrayPageScroll[0] + (pagesize[0] - 882)/2), top: (arrayPageScroll[1] + (pagesize[1]-500)/2)};
	$("#lightbox").css(style);
}

function overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	$("#overlay").css({"height": yScroll, "width": xScroll});
	$("#hide_select").css({"height": yScroll,"width": xScroll});
}

function loadPosition() {
	var pagesize = getPageSize();
	var arrayPageScroll = getPageScrollTop();
	$("#load")
		.css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2), top: (arrayPageScroll[1] + ((pagesize[1]-100)/2)) })
		.css({display:"block"});
}

function TB_parseQuery ( query ) {
	// return empty object
	if( !query )
		return {};
	var params = {};

	// parse query
	var pairs = query.split(/[;&]/);
	for ( var i = 0; i < pairs.length; i++ ) {
		var pair = pairs[i].split('=');
		if ( !pair || pair.length != 2 )
			continue;
		// unescape both key and value, replace "+" with spaces in value
		params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
   }
   return params;
}

function getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop)
	return arrayPageScroll;
}

function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h)
	return arrayPageSize;
}

