// JavaScript Document

function errorOutput(errorHtml){
	$('shuttererror').hide();
	if(typeof(errorHtml) != "undefined" && errorHtml != ""){
		$('shuttererrortext').innerHTML = errorHtml;
		$('shuttererror').show();
	}
}

function capturelinks() {

	if (!document.all) {
//		$('pageid175').onclick = function (e) {loadtab(175);return false;};
		$('pageid175').onclick = function (e) {loadtab(175);return false;};
		$('pageid176').onclick = function (e) {loadtab(176);return false;};
//		$('pageid999').onclick = function (e) {loadtab(999);return false;};
		$('pageid177').onclick = function (e) {loadtab(177);return false;};
		$('pageid178').onclick = function (e) {loadtab(178);return false;};
	}

}
function capturetablinks(pageid) {

	$('pageid175').removeClassName('subon');
	$('pageid176').removeClassName('subon');
//	$('pageid999').removeClassName('subon');
	$('pageid177').removeClassName('subon');
	$('pageid178').removeClassName('subon');

	$(pageid).addClassName('subon');

	if (!document.all) {
		$('tabid175').onclick = function (e) {loadtab(175);return false;};
		$('tabid176').onclick = function (e) {loadtab(176);return false;};
//		$('tabid999').onclick = function (e) {loadtab(999);return false;};
		$('tabid177').onclick = function (e) {loadtab(177);return false;};
		$('tabid178').onclick = function (e) {loadtab(178);return false;};

		if($('linkid176')){
			$('linkid176').onclick = function (e) {loadtab(176);return false;};
		}
//		if($('linkid999')){
//			$('linkid999').onclick = function (e) {loadtab(999);return false;};
//		}
		if($('linkid177')){
			$('linkid177').onclick = function (e) {loadtab(177);return false;};
		}
	}
}
function onTick() {
	timerCookie = null;

	if (!document.all) {
		if(document.location.hash == "" && currentpageid != 175){
			loadtab(175);
		}
	}
	if(document.location.hash == "#SizeandDesign" && currentpageid != 175){
		loadtab(175);
	}
	if(document.location.hash == "#PanelSlats" && currentpageid != 176){
		loadtab(176);
	}
//	if(document.location.hash == "#ColourWoodType" && currentpageid != 999){
//		loadtab(999);
//	}
	if(document.location.hash == "#InstallationFinish" && currentpageid != 177){
		loadtab(177);
	}
	if(document.location.hash == "#Help" && currentpageid != 178){
		loadtab(178);
	}
	timerCookie = window.setTimeout(onTick, 200);
}
function loadtab(pageid) {
	currentpageid = pageid;
	new Ajax.Request('/sitestyle/modules/code/shuttersmb/tabloader.php?pageid='+pageid, {onSuccess: loadtabdispay});
	switch (pageid) {
		case 175:
			window.location.hash = "SizeandDesign";
			break;
		case 176:
			window.location.hash = "PanelSlats";
			break;
//		case 999:
//			window.location.hash = "ColourWoodType";
//			break;
		case 177:
			window.location.hash = "InstallationFinish";
			break;
		case 178:
			window.location.hash = "Help";
			break;
	}
	return false;
}
function loadtabdispay(transport) {
	$('tabszone').replace(transport.responseText);
	$$('.columns').each(function(item) { item.style.height = "";});
}

function unitswitch(unit) {
	Delete_Cookie('defaultunits');
	Delete_Cookie('defaultunits', '/', 'theshutterstore.com' );
	Set_Cookie( 'defaultunits', unit, 60, '/', '.www.theshutterstore.com' );
	if($('unitesmm')) {
		if(unit == 'mm'){
			$('unitesmm').style.display = '';
			$('unitesinch').style.display = 'none';
		}
		else {
			$('unitesmm').style.display = 'none';
			$('unitesinch').style.display = '';
		}
	}
	drawlayout();
	detaillayout();
}

function railstyle(heightmm) {
	if (heightmm >= 500) {
		$('typeFullHeightwithRail').show();
		$('typeFullHeightwithRailOff').hide();
		$('typeCafeStylewithRail').show();
		$('typeCafeStylewithRailOff').hide();
	} else {
		$('typeFullHeightwithRail').hide();
		$('typeFullHeightwithRailOff').show();
		$('typeCafeStylewithRail').hide();
		$('typeCafeStylewithRailOff').show();
	}
	if (heightmm >= 1800) {
		$('typeFullHeight').hide();
		$('typeFullHeightOff').show();
		$('typeCafeStyle').hide();
		$('typeCafeStyleOff').show();
	} else {
		$('typeFullHeight').show();
		$('typeFullHeightOff').hide();
		$('typeCafeStyle').show();
		$('typeCafeStyleOff').hide();
	}
}

function sizevalue(transport) {
	$('shuttererror').style.display = "NONE";
	if(transport){
		var sizes = transport.responseText.evalJSON(true);
		switch(sizes.field) {
			case 'widthmm':
			  $('widthmm').innerHTML = sizes.value;
			  $('widthin').innerHTML = sizes.value2;
			  break;
			case 'widthin':
			  $('widthin').innerHTML = sizes.value;
			  $('widthmm').innerHTML = sizes.value2;
			  break;
			case 'heightmm':
			  $('heightmm').innerHTML = sizes.value;
			  $('heightin').innerHTML = sizes.value2;
			  railstyle(sizes.value);
			  break;
			case 'heightin':
			  $('heightin').innerHTML = sizes.value;
			  $('heightmm').innerHTML = sizes.value2;
			  railstyle(sizes.value2);
			  break;
			case 'dividerpositionmm':
			  $('dividerpositionmm').innerHTML = sizes.value;
			  $('dividerpositionin').innerHTML = sizes.value2;
			  break;
			case 'dividerpositionin':
			  $('dividerpositionin').innerHTML = sizes.value;
			  $('dividerpositionmm').innerHTML = sizes.value2;
			  break;
		}
	}
	if (sizes.type != "") {
		$("type"+sizes.type).siblings().each(function(s) {s.removeClassName('imagechoice-on');});
		$("type"+sizes.type).addClassName('imagechoice-on');
	}
	drawlayout();
	detaillayout();
	errorOutput(sizes.error);
};

Object.extend(Ajax.InPlaceEditor.prototype, {
	getText: function() {
		return this.element.innerHTML.stripTags();
	}
});

function sectionaction(action) {
	new Ajax.Request('/sitestyle/modules/code/shuttersmb/sectionchange.php?action='+action, {onSuccess: sectiondisplay});
}
function sectiondisplay(transport) {
	var sectioncode = transport.responseText.evalJSON(true);
	$('sectionlayout').replace(sectioncode.layout);
	if(sectioncode.remove == 1) {
		$('sectionremove').style.display = "none";
	}
	else {
		$('sectionremove').style.display = "";
	}
	if(sectioncode.add == 1) {
		$('sectionadd').style.display = "none";
	}
	else {
		$('sectionadd').style.display = "";
	}
	$('sectioncount').innerHTML = sectioncode.count;
	drawlayout();
	detaillayout();
	errorOutput(sectioncode.error);
}



function drawlayout() {
	new Ajax.Request('/sitestyle/modules/code/shuttersmb/drawlayout.php', {onSuccess: drawlayoutdisplay});
}
function drawlayoutdisplay(transport) {

	var shutter = transport.responseText.evalJSON(true);

	switch(shutter.colour) {
		case 'White':
			$('closeupfinish').style.backgroundColor = '#ffffff';
			$('closeupfinish').innerHTML = '<span>White</span>';
		  break;
		case 'Shell':
			$('closeupfinish').style.backgroundColor = '#F2F0E1';
			$('closeupfinish').innerHTML = '<span>Shell</span>';
		  break;
		case 'Pearl':
			$('closeupfinish').style.backgroundColor = '#F2F4E9';
			$('closeupfinish').innerHTML = '<span>Pearl</span>';
		  break;
		case 'Biscuit':
			$('closeupfinish').style.backgroundColor = '#F3F3E7';
			$('closeupfinish').innerHTML = '<span>Biscuit</span>';
		  break;
		default:
			$('closeupfinish').innerHTML = '<span>'+shutter.colour+'</span>';
			$('closeupfinish').style.backgroundColor = '#'+shutter.colorhex;
			shutter.colour = shutter.colorhex;
	}

	flash = $('shutterlayout');

	if(flash.drawShutterLayout) {
		flash.drawShutterLayout(shutter.width,shutter.height,shutter.dividerposition,shutter.type,shutter.left,shutter.right,shutter.colour,shutter.hingecolour,shutter.slatsize,shutter.pushrod,shutter.installation);
	}
	else {
		$('shutterlayoutembed').drawShutterLayout(shutter.width,shutter.height,shutter.dividerposition,shutter.type,shutter.left,shutter.right,shutter.colour,shutter.hingecolour,shutter.slatsize,shutter.pushrod,shutter.installation);
	}
}
function detaillayout() {
	new Ajax.Request('/sitestyle/modules/code/shuttersmb/detailview.php', {onSuccess: function(transport) { $('detailview').replace(transport.responseText);}});
}
function listlayout() {
	new Ajax.Request('/sitestyle/modules/code/shuttersmb/listview.php', {onSuccess: function(transport) { $('listview').replace(transport.responseText);}});
}


function buttonchange(field,action) {
	new Ajax.Request('/sitestyle/modules/code/shuttersmb/buttonchange.php?button='+field+'&value='+action, {onSuccess: buttondisplay});
}
function buttondisplay(transport) {
	var btcode = transport.responseText.evalJSON(true);
	if(btcode.button != ""){
		$(btcode.button+btcode.value).siblings().each(function(s) {s.style.display = "none";});
		$(btcode.button+btcode.value).style.display = "";
	}
	drawlayout();
	detaillayout();

}

var colorComapany = '';
var colorName = '';

function choicechange(field,action) {

	idString = field + action.replace(/ /g, "");
	if(!$(idString).hasClassName('imagechoice-on')){
		new Ajax.Request('/sitestyle/modules/code/shuttersmb/choicechange.php?choice='+field+'&value='+action, {onSuccess: choicedisplay});
	}
}
function choicedisplay(transport) {
	var btcode = transport.responseText.evalJSON(true);
	if(btcode.choice != ""){

		$(btcode.choice+btcode.value).siblings().each(function(s) {s.removeClassName('imagechoice-on');});
		$(btcode.choice+btcode.value).addClassName('imagechoice-on');

		if(btcode.name != ""){
			$('Color_Search').value = btcode.name;
			$('currentColor').style.backgroundColor = '#'+btcode.hex;
		}
		else {
			$('doc').scrollTo();
		}
	}
	if (btcode.choice == "slatsize") {
		sectionaction('');
	} else {
		drawlayout();
		detaillayout();
	}
	errorOutput(btcode.error);
}
function setchoice(choice) {
	if($(choice)) $(choice).addClassName('imagechoice-on');
	if(choice == 'sidesNoFrames'){
		$('frame_installation').hide();
	}
}
function setcolorchoice(choice,company,name,hex) {
	if($(choice)) $(choice).addClassName('imagechoice-on');

	if(name != ""){
		colorComapany = company;
		$('Color_Search').value = name;
		$('currentColor').style.backgroundColor = '#'+hex;
		$('moreColorChoice').style.display = "";
	}
}

function moreColorChoiceSelect(element, entry) {
	// alert(entry + "&searchComapany=" + colorComapany);
	return entry + "&searchComapany=" + colorComapany;
}
function moreColorChoiceSelected(text, id) {
	$('currentColor').style.backgroundColor = id.style.borderLeftColor;
	new Ajax.Request('/sitestyle/modules/code/shuttersmb/choicechange.php?choice=colour&value='+id.title, {onSuccess: moreColorChoiceDisplay});
}
function moreColorChoiceDisplay(transport) {
	var btcode = transport.responseText.evalJSON(true);
	$('doc').scrollTo();
	drawlayout();
	detaillayout();
	errorOutput(btcode.error);
}
