﻿$(document).ready( function() {
	
	// loadSearchState
	/*
	var style = $("select[name=style] option[selected]").attr("value");
	var environment = $("select[name=environment] option[selected]").attr("value");
	var layer = $("select[name=layer] option[selected]").attr("value");
	var windows = $("select[name=windows] option[selected]").attr("value");
	*/
	
	initSearchFields();
	updateSearchNav();
	
	var version = $("#doorSettings").attr("version");
	
	if(version == "js"){
		reloadDoors();
		initJSArrows();
	}
	else
		reloadFlash();
});


function initSearchFields(){
	
	$("form[name=searchDoors] select").change( function() {
		$(this).attr("selected","true")
		reLoadOptions();
	});

}

function reLoadOptions() {
	
	var style = $("select[name=style] option:selected").attr("value");
	var environment = $("select[name=environment] option:selected").attr("value");
	var layer = $("select[name=layer] option:selected").attr("value");
	var langId = $("div.searchForm").attr('langId');
	$.post("ajax/reLoadOptions.lasso", { style: style, environment: environment, langId: langId },
	function(data) {
		var dataContent = data.split('\n');
		var found = false;
		environments = dataContent[0].split(";");
		layers = dataContent[1].split(";");
		
		// Update Environments
		var defaultEnvironment = $('select[name=environment] option[default=true]').html();
		var newHtml = '<option value="all" default="true">' + defaultEnvironment + '</option>\n';
		found = false;
		for(var i in environments){
			if(environments[i] != ""){
				if(environments[i] == environment){
					newHtml += '<option value="' + environments[i] + '" selected >' + environments[i] + '</option>\n';
					found = true;
				}
				else
					newHtml += '<option value="' + environments[i] + '">' + environments[i] + '</option>\n';
			}
		}
		$('select[name=environment]').html(newHtml);
		if(found == false)
			$('select[name=environment] option[default=true]').attr("selected","selected");
		
		// Update layers
		var defaultLayer = $('select[name=layer] option[default=true]').html();
		var newHtml = '<option value="all" default="true">' + defaultLayer + '</option>\n';
		found = false;
		for(var i in layers){
			if(layers[i] != ""){
				if(layers[i] == layer){
					newHtml += '<option value="' + layers[i] + '" selected>' + layers[i] + '</option>\n';
					found = true;
				}
				else
					newHtml += '<option value="' + layers[i] + '" >' + layers[i] + '</option>\n';
			}
		}
		$('select[name=layer]').html(newHtml);
		if(found == false)
			$('select[name=layer] option[default=true]').attr("selected","selected");
			
		
		updateSearchNav();
		var version = $("#doorSettings").attr("version");
	
		if(version == "js")
			reloadDoors();
		else
			reloadFlash();
	});
		
}




function updateSearchNav() {
	
	
	var style = $("select[name=style] option:selected").attr("value");
	var environment = $("select[name=environment] option:selected").attr("value");
	var layer = $("select[name=layer] option:selected").attr("value");
	var windows = $("select[name=windows] option:selected").attr("value");
	var windowsText =  $("select[name=windows] option:selected").html();
	
	$("div.content-navText").html('<a href="javascript:resetOptions(1)">' + $("div.content-navText").attr('default') + '</a>');
	
	if(style != "" && style != "all"){
		if(environment == "all" && windows == "all" && layer == "all")
			$("div.content-navText").html($("div.content-navText").html() + ' <img src="images2/smalArrow.png" alt="" valign="top"/> ' + style + '');
		else
			$("div.content-navText").html($("div.content-navText").html() + ' <img src="images2/smalArrow.png" alt="" valign="top" />  <a href="javascript:resetOptions(2)">' + style + '</a>');
	}
	
	if(environment != "" && environment != "all"){
		if(windows == "all" && layer == "all")
			$("div.content-navText").html($("div.content-navText").html() + ' <img src="images2/smalArrow.png" alt="" valign="top" />  ' + environment + '');
		else
			$("div.content-navText").html($("div.content-navText").html() + ' <img src="images2/smalArrow.png" alt="" valign="top" />  <a href="javascript:resetOptions(3)">' + environment + '</a>');
	}
	
	if(layer != "" && layer != "all"){
		if(windows == "all")
			$("div.content-navText").html($("div.content-navText").html() + ' <img src="images2/smalArrow.png" alt="" valign="top" />  ' + layer + '');
		else
			$("div.content-navText").html($("div.content-navText").html() + ' <img src="images2/smalArrow.png" alt="" valign="top" />  <a href="javascript:resetOptions(4)">' + layer + '</a>');
	}

	if(windows != "" && windows != "all"){
			$("div.content-navText").html($("div.content-navText").html() + ' <img src="images2/smalArrow.png" alt="" valign="top" />  ' + windowsText);
	}
	
}


function resetOptions( level ){
	if(level == 1){
		$("select[name=style] option[default=true]").attr("selected","selected");
		$("select[name=environment] option[default=true]").attr("selected","selected");
		$("select[name=layer] option[default=true]").attr("selected","selected");
		$("select[name=windows] option[default=true]").attr("selected","selected");		
	}
	
	if(level == 2){
		$("select[name=environment] option[default=true]").attr("selected","selected");
		$("select[name=layer] option[default=true]").attr("selected","selected");
		$("select[name=windows] option[default=true]").attr("selected","selected");		
	}
	
	if(level == 3){
		$("select[name=layer] option[default=true]").attr("selected","selected");
		$("select[name=windows] option[default=true]").attr("selected","selected");		
	}
	
	if(level == 4){
		$("select[name=windows] option[default=true]").attr("selected","selected");		
	}
	
	updateSearchNav();
	var version = $("#doorSettings").attr("version");
	
	if(version == "js")
		reloadDoors();
	else
		reloadFlash();
		
	reLoadOptions();
}

function reloadFlash(){
		
		
		var style = $("select[name=style] option:selected").attr("value");
		var environment = $("select[name=environment] option:selected").attr("value");
		var layer = $("select[name=layer] option:selected").attr("value");
		var windows = $("select[name=windows] option:selected:").attr("value");
		var langId = $("div.searchForm").attr('langId');
		
		
		var newUrl = "doors.lasso?langId=";
		newUrl += langId;
		newUrl += '%26style=' + style;
		newUrl += '%26environment=' + environment;
		newUrl += '%26layer=' + layer;
		newUrl += '%26windows=' + windows;
		newUrl += '%26output=XML';
		
		
		
		var flashHtml = '<div class="content-navFrame" id="doors-flash">'
							+ 'Du saknar flash i din webbläsare.<br>'
							+ '<a href="http://get.adobe.com/se/flashplayer/?promoid=BUIGP">Hämta flashplayer</a>'
						+ '</div>'
						
						+ '<script type="text/javascript">'
							+ 'var so = new SWFObject("doors.swf", "doors-flash-elem", "1024", "200", "9", "#FFFFFF");'
							+ 'so.addParam("name","doors-flash");'
							+ 'so.addParam("menu","false");'
							+ 'so.addParam("wmode","transparent");'
							+ 'so.addParam("allowscriptaccess","always");'
							+ 'so.addParam("scale","noorder");'
							+ 'so.addVariable("javascriptid","doors-flash");'
							+ 'so.addVariable("enablejs","true");'
							+ 'so.addVariable("xml","' + newUrl + '");'
							+ 'so.write("doors-flash");'
						+ '</script>';
						
		$(".flashContainer").html(flashHtml);
}

function reloadDoors() {
	
	var style = $("select[name=style] option:selected").attr("value");
	var environment = $("select[name=environment] option:selected").attr("value");
	var layer = $("select[name=layer] option:selected").attr("value");
	var windows = $("select[name=windows] option:selected:").attr("value");
	var langId = $("div.searchForm").attr('langId');
	
	
	var newUrl = "doors.lasso?langId=";
	newUrl += langId;
	newUrl += '&style=' + style;
	newUrl += '&environment=' + environment;
	newUrl += '&layer=' + layer;
	newUrl += '&windows=' + windows;
	newUrl += '&output=JS';
	
	$.get(newUrl, function(data) {
		$("#doors-js #content").html(data);
		var i = 0;
		$("#doors-js #content img").each( function() {
			
			$(this).click( function() {
				document.location=$("base").attr("href") + $(this).attr("link");
			});
			
			if(i < 8){
				$(this).show();
				$(this).attr("display",true);
			}
			else
				$(this).attr("display",false);
			
			$(this).height("180");
			i++;
		});
	});
	
	
	
}

function initJSArrows() {
	
	$("#doors-js #arrowRight").click( function() {
		moveImagesLeft();
	});
	
	$("#doors-js #arrowLeft").click( function() {
		moveImagesRight();
	});
	

}

function moveImagesLeft(){
	var i = 1;
	$("#doors-js #content img[display=true]").each( function() {
		if($("#doors-js #content img").last().attr("display") != 'true'){
			if(i == 1){
				$(this).hide();
				$(this).attr("display",false);
			}
			
			if(i == 8){
					$(this).next('img').show();
					$(this).next('img').attr("display",true);
			}
			i++;
		}
	});
}

function moveImagesRight(){
	var i = 1;
	$("#doors-js #content img[display=true]").each( function() {
		if($("#doors-js #content img").first().attr("display") != 'true'){
			if(i == 8){
				$(this).hide();
				$(this).attr("display",false);
			}
			
			if(i == 1){
					$(this).prev('img').show();
					$(this).prev('img').attr("display",true);
			}
			i++;
		}
	});
}




