$(window).load(function(){
	path = location.pathname;
	root = false;
	if ( path === "/han/" || path === "/index.html" ) {
		root = true;
	}
	/*******************************
		Selector
	*******************************/	
	
	$("#selector").find("li").click(function(){
		var index = $(this).index();
		$("#selector .sel").removeClass("sel");
		$(this).children("a").addClass("sel");
		$("#img_holder").attr("src", glasses[index] );
	});
	
	
	/*******************************
		Variants
	*******************************/		
	$("#variants").find("a").click(function(){
		$("#variants .sel").removeClass("sel");
		$(this).addClass("sel");
		
		// change pic goes here
	});
	
		
	/*******************************
		Header
	*******************************/
	$("#header").click(function(){
		if ( !root ) {
			window.location = "../";
		}else{
			window.location = "./";
		}
	});
	
	/*******************************
		News
	*******************************/
	//$("#newsBody").load("news.html #news");
	$("#newsBody").empty();	
	api = $('#newsBody').jScrollPane({
    	showArrows:false,
    	maintainPosition: false
    }).data('jsp');

	if ( !root ) {    	
	    api.getContentPane().load(
    		"../news.html?v=3 #news",
    		function() {
    			api.reinitialise();
	    });
	}else{
	    api.getContentPane().load(
    		"news.html?v=3 #news",
    		function() {
    			api.reinitialise();
	    });
	
	}
    
    
});


    
/*******************************
	Glasses
*******************************/

function loadGallery(name) {
	/*
	var titles = [];

	$.getJSON( name + ".json", function(data){
		$.each(data, function(i, item) {				
			temp_title = item[0].src.slice(0,-4);
			temp_title = temp_title.replace(/_/g," ");
			var variant_check = temp_title.substr(temp_title.length-2);				
			titles.push(temp_title);				    			    			
		});
	
		len = titles.length;
		$("#selector").find("ul").empty();
		
		for ( i=0;i<len;i++ ) {
				$("#selector").find("ul").append("<li><a href='javascript:void;'>"+titles[i]+"</a></li>");
		}				
				
		// Trigger first item
		$("#selector li").first().click();	
	});
	*/
}

function loadFrames(name) {
	var titles = [];
	var variants = [];

	$.getJSON( name + ".json", function(data){

		$.each(data, function(i, item) {				
			temp_title = item[0].src.slice(0,-4);
			temp_title = temp_title.replace(/_/g," ");
			var variant_check = temp_title.substr(temp_title.length-2);					
			if ( variant_check === " S" || variant_check === " C") {
				temp_title = temp_title.slice(0,-2);
			}								
			titles.push(temp_title);				    			    												
		});
	
		len = titles.length;
		$("#selector").find("ul").empty();
		
		for ( i=0;i<len;i++ ) {
				$("#selector").find("ul").append("<li><a href='javascript:void;'>"+titles[i]+"</a></li>");
		}				
		
		$("#selector").find("li").click(function(){
			var test = $(this).text();
			var test = test.replace(/ /g,"_");			
			
			var len = data[test].length;
			$("#img_holder").html("<div id='loading'>Loading</div>");

			function placeImg(){
				$("#img_holder").html("<img src='"+ data[test][0].src +"' width='684' height='300' />");
				clearTimeout(tid);
			}
			tid = setTimeout(placeImg, 200);
		
			
			if ( len > 1 ) {
				$("#variants").show();
				$("#variants .sel").removeClass("sel");
				$("#variants #sun").addClass("sel");
				
				variants = [];
				for ( var i=0; i<len;i++) {
					variants.push( data[test][i]);
				}										
			}else{
				$("#variants").hide();
			}
			
			$("#selector .sel").removeClass("sel");
			$(this).children("a").addClass("sel");					
		});

		$("#variants #clear").click(function(){
			$("#img_holder").html("<div id='loading'>Loading</div>");
			function placeImg(){
				$("#img_holder").html("<img src='"+ variants[1].src +"' width='684' height='300' />");
				clearTimeout(tid);
			}
			tid = setTimeout(placeImg, 200);			
			$("#variants .sel").removeClass("sel");
			$(this).addClass("sel");
		});

		$("#variants #sun").click(function(){
			$("#img_holder").html("<div id='loading'>Loading</div>");
			function placeImg(){
				$("#img_holder").html("<img src='"+ variants[0].src +"' width='684' height='300' />");			
				clearTimeout(tid);
			}
			tid = setTimeout(placeImg, 200);	
			$("#variants .sel").removeClass("sel");
			$(this).addClass("sel");
		});			
		
		// Trigger first item
		$("#selector li").first().click();
		
		$("#blueprint").click(function(){
    		$("#selector .sel").removeClass("sel");
    		$("#variants").hide();
    		$("#img_holder").html("<img src='blueprint.jpg' width='684' height='285' />");
    		$("#blueprint").addClass("sel");
    	});		
	});

}
