var tab;
var i;
var req = new String(window.location);
var qs1 = req.split("#",2);
qs1.push(' ');
var qs2 = qs1[1].split("_",2);

var reqTab = qs2[0];
var reqImg = qs2[1];

$(document).ready(function(){
	
	//alert("Requesting tab: "+reqTab+" and image: "+reqImg);
	
	//Prep App Icons Section
	for(i=0;i<$("a[rel=preview1]").length;i++) {
		$("a[rel=preview1]").eq(i).attr('destination',$("a[rel=preview1]").eq(i).attr('href'));
		$("a[rel=preview1]").eq(i).click(function() {
			$("#preview1 img").attr('src',$(this).attr('destination'));
		});
		$("a[rel=preview1]").eq(i).attr('href','#');
	}
	
	//Prep iPhone Icons Section
	for(i=0;i<$("a[rel=preview2]").length;i++) {
		$("a[rel=preview2]").eq(i).attr('destination',$("a[rel=preview2]").eq(i).attr('href'));
		$("a[rel=preview2]").eq(i).click(function() {
			$("#preview2 img").attr('src',$(this).attr('destination'));
		});
		$("a[rel=preview2]").eq(i).attr('href','#');
	}
	
	//Prep Logos Section
	for(i=0;i<$("a[rel=preview3]").length;i++) {
		$("a[rel=preview3]").eq(i).attr('destination',$("a[rel=preview3]").eq(i).attr('href'));
		$("a[rel=preview3]").eq(i).click(function() {
			$("#preview3 img").attr('src',$(this).attr('destination'));
		});
		$("a[rel=preview3]").eq(i).attr('href','#');
	}
	
	//Determine which tab we should be on and get us there.
	if(reqTab == 'iphone') {
		tab = 2;
		$("#top1").animate({width:'421px',minWidth:'421px',maxWidth:'421px',marginRight:'15px'},0);
		$("#top2").animate({width:'187px',minWidth:'187px',maxWidth:'187px'},0);
		$("#preview1").hide();
		$("#selector1").hide();
		$("#preview3").hide();
		$("#selector3").hide();
		$("#preview4").hide();
		$("#selector4").hide();
		$("#tab_app").css({zIndex:'20',backgroundImage:'url(images/nav_idle.png)'});
		$("#tab_iphone").css({zIndex:'30',backgroundImage:'url(images/nav_active.png)'});
		$("#tab_logos").css({zIndex:'10',backgroundImage:'url(images/nav_idle.png)'});
	}
	else if(reqTab == 'logo') {
		tab = 3;
		$("#top2").animate({width:'157px',minWidth:'157px',maxWidth:'157px'},0);
		$("#top1").animate({width:'451px',minWidth:'451px',maxWidth:'451px',marginRight:'17px'},0);
		$("#preview1").hide();
		$("#selector1").hide();
		$("#preview2").hide();
		$("#selector2").hide();
		$("#preview4").hide();
		$("#selector4").hide();
		$("#tab_app").css({zIndex:'20',backgroundImage:'url(images/nav_idle.png)'});
		$("#tab_iphone").css({zIndex:'10',backgroundImage:'url(images/nav_idle.png)'});
		$("#tab_logos").css({zIndex:'30',backgroundImage:'url(images/nav_active.png)'});
	}
	else {
		tab = 1;
		$("#top2").animate({width:'157px',minWidth:'157px',maxWidth:'157px'},0);
		$("#top1").animate({width:'451px',minWidth:'451px',maxWidth:'451px',marginRight:'17px'},0);
		$("#preview2").hide();
		$("#selector2").hide();
		$("#preview3").hide();
		$("#selector3").hide();
		$("#preview4").hide();
		$("#selector4").hide();
		$("#tab_app").css({zIndex:'30',backgroundImage:'url(images/nav_active.png)'});
		$("#tab_iphone").css({zIndex:'20',backgroundImage:'url(images/nav_idle.png)'});
		$("#tab_logos").css({zIndex:'10',backgroundImage:'url(images/nav_idle.png)'});
	}
	
	//Set click functions for the tabs.
	$("#tab_app").click( function(){
		$("#top2").animate({width:'157px',minWidth:'157px',maxWidth:'157px'},700);
		$("#top1").animate({width:'451px',minWidth:'451px',maxWidth:'451px',marginRight:'17px'},700);
		$("#tab_app").css({zIndex:'30',backgroundImage:'url(images/nav_active.png)'});
		$("#tab_iphone").css({zIndex:'20',backgroundImage:'url(images/nav_idle.png)'});
		$("#tab_logos").css({zIndex:'10',backgroundImage:'url(images/nav_idle.png)'});
		if(tab != 1) {
			$("#preview"+tab).fadeOut(600);
			$("#selector"+tab).fadeOut(600);
			tab = 1;
			setTimeout('$("#preview"+tab).fadeIn(600);$("#selector"+tab).fadeIn(600);',630);
		}
	});
	$("#tab_iphone").click( function(){
		$("#top1").animate({width:'421px',minWidth:'421px',maxWidth:'421px',marginRight:'15px'},700);
		$("#top2").animate({width:'187px',minWidth:'187px',maxWidth:'187px'},700);
		$("#tab_app").css({zIndex:'20',backgroundImage:'url(images/nav_idle.png)'});
		$("#tab_iphone").css({zIndex:'30',backgroundImage:'url(images/nav_active.png)'});
		$("#tab_logos").css({zIndex:'10',backgroundImage:'url(images/nav_idle.png)'});
		$("#lower_left").fadeOut(600);
		$("#lower_right").fadeOut(600);
		
		if(tab != 2) {
			$("#preview"+tab).fadeOut(600);
			$("#selector"+tab).fadeOut(600);
			tab = 2;
			setTimeout('$("#preview"+tab).fadeIn(600);$("#selector"+tab).fadeIn(600);',630);
		}
	});
	$("#tab_logos").click( function(){
		$("#top2").animate({width:'157px',minWidth:'157px',maxWidth:'157px'},700);
		$("#top1").animate({width:'451px',minWidth:'451px',maxWidth:'451px',marginRight:'17px'},700);
		$("#tab_app").css({zIndex:'20',backgroundImage:'url(images/nav_idle.png)'});
		$("#tab_iphone").css({zIndex:'10',backgroundImage:'url(images/nav_idle.png)'});
		$("#tab_logos").css({zIndex:'30',backgroundImage:'url(images/nav_active.png)'});
		$("#lower_left").fadeOut(600);
		$("#lower_right").fadeOut(600);
		
		if(tab != 3) {
			$("#preview"+tab).fadeOut(600);
			$("#selector"+tab).fadeOut(600);
			tab = 3;
			setTimeout('$("#preview"+tab).fadeIn(600);$("#selector"+tab).fadeIn(600);',630);
		}
	});
});
