// JavaScript Document
var timerCookie;

function openContactUs(){
	$('openContactUs').style.display = "none";
	$('closeContactUs').style.display = "";
	new Effect.BlindDown($('contactustrans'));
}
function closeContactUs(){
	$('closeContactUs').style.display = "none";
	$('openContactUs').style.display = "";
	new Effect.BlindUp($('contactustrans'));
}

function openAreasCovered(){
	$('openAreasCovered').style.display = "none";
	$('closeAreasCovered').style.display = "";
	new Effect.BlindDown($('areascoveredtrans'));
}
function closeAreasCovered(){
	$('closeAreasCovered').style.display = "none";
	$('openAreasCovered').style.display = "";
	new Effect.BlindUp($('areascoveredtrans'));
}
function featureOver(id,pos){
	id.style.backgroundPosition = pos;
	id.style.cursor = "pointer";
}
function featureOver2(id,pos){
	id.style.backgroundPosition = pos;
}
function featureOut(id,pos){
	id.style.backgroundPosition = pos;
	id.style.cursor = "";
}
function featureClick(id){
	link = $(id).down('a');
	if(link != undefined) {
		location.href = link.href;
	}
}
function headermenu(item){
	if($(item)){
		$(item).addClassName('header-menu-on');
	}
}
function continueShoppingB(cSLink){
	switch (cSLink) {
		case 'blinds':
			$('moreblinds').style.display = "";
		break;
		case 'shutters':
			$('moreshutters').style.display = "";
		break;
	}
}
function additem(itemid){
	
	quty = $('quantity'+itemid).value;

	new Ajax.Request('/modules/shop/add.php', {
	  method: 'get',
	  parameters: {dataid: itemid, orderquantity: quty},
	  onSuccess: cartchanged
	  });
	
}
function cartchanged(transport){
	$('products_cartnote').style.display = "";
	setTimeout("$('products_cartnote').style.display = 'none';", 2000 );
}
function onLoad() {
	// this doesn't help, Opera is currently broken:
	if (window.opera && window.history) {
		history.navigationMode = 'compatible';
	}
	// Set-up the polling:
	timerCookie = window.setTimeout(onTick, 200);
}
function onUnload() {
	if (timerCookie) {
		window.clearTimeout(timerCookie);
	}
}
function galleryZoom(id) {

	imagebox = "gallery_image_"+id;
	imagelink = $(imagebox).select('a');
	popUp(imagelink[0].href,700,700);
//	eval("page" + id + " = window.open('" + imagelink[0].href + "', 'page" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=300,height=400');");
//	window.location.href = imagelink[0].href;
}

function temporder(width) {
	
	tempsmall = 0;
	
	templarge = Math.floor(width / 1210);
	
	widthleft = width - (templarge * 1210);
	
	if(widthleft < 910) {
		tempsmall = 1;
	}
	else {
		templarge = templarge + 1;
	}
	baskettempsmall += tempsmall;
	baskettemplarge += templarge;
}

function tempExtraOrder() {
	
	baskettempsmall = baskettempsmall - dataid[99];
	baskettemplarge = baskettemplarge - dataid[100];
	
	if(baskettempsmall > 0 || baskettemplarge > 0) {
		$('extraTemps').style.display = "";
		extraTempsTotal = 0;
		if(baskettempsmall > 0)	{
			extraTempsTotal = baskettempsmall * 5;
		}
		if(baskettemplarge > 0)	{
			extraTempsTotal = extraTempsTotal + (baskettemplarge * 8);
		}
//		$('extraTempsPrice').innerHTML = extraTempsTotal;
	}
	
	
}
function getDataQuantity(basid) {
	if($('quantity['+basid+']')) {
		return $('quantity['+basid+']').value;
	}
}

function tempExtraOrderAdd() {
	
	if(baskettempsmall > 0)	{
		new Ajax.Request('/modules/shop/add.php', {method: 'get', parameters: {dataid: 99, orderquantity: baskettempsmall}});
	}
	if(baskettemplarge > 0)	{
		new Ajax.Request('/modules/shop/add.php', {method: 'get', parameters: {dataid: 100, orderquantity: baskettemplarge}});
	}
	setTimeout("window.location = '/Basket/'", 1000 )
}


var wait = true;
var displayTime = 2;
var speed = 8;
var scrollitem;
var currentitem = 0;
var totalitems;
var back = false;

function scrollcasestudies(){
	var position = parseInt(scrollitem.style.left);
	if(!wait){
		if(back)
  			position = position + speed;
  		else
 			position = position - speed;
  		
  		scrollitem.style.left = position + "px";
  		
  		if(back){
	 		if((currentitem * -100) < position){
	 			scrollitem.style.left = (currentitem * -100) + "px";
	 			if(currentitem == 0)
	 				back = false;
				wait = true;
			}
		}
		else {
	 		if((currentitem * -100) > position){
	 			scrollitem.style.left = (currentitem * -100) + "px";
	 			if(currentitem == (totalitems - 1))
	 				back = true;
				wait = true;
			}
		}
		setTimeout("scrollcasestudies()",100);
    } else{
    	if(back)
    		currentitem--;
    	else
   			currentitem++;
    		
   	    wait = false;
		setTimeout("scrollcasestudies()", displayTime * 1000);
	}
}

function scrollcasestudiesInit(lenght){
	totalitems = lenght;
	scrollitem = document.getElementById('casestudiesscroll');
	scrollitem.style.left = "0px";
	scrollitem.style.width = (100 * lenght) + "px";
	scrollcasestudies();
}
function loadQuestion(id) {
	$('hank_text').replace('<div id="hank_text">'+$('faqA'+id).innerHTML+'</div>');
	
	$$('.questionanswer_list li a').each(function(value, index) { value.removeClassName('select');});
	$("faqQ"+id).addClassName('select');

}
function loadFirstQuestion() {
	questions = $$('.questionanswer_list li a');
	loadQuestion(questions[0].id.replace(/faqQ/, ""))
}

function hookMovies() {
//	$$('a[href~="movies"]').each(function(value, index) { value.observe('click', openMoviePanel); alert('H');});

	$$('a').each(function(value, index) { if(value.href.search(/\/sitestyle\/movies\/index.php/) != -1) {value.observe('click', openMoviePanel);value.target = "";} });
}
//addLoadEvent(hookMovies);

function openMoviePanel(event) {
	var element = event.element();
	if(!element.href){
		element = event.findElement('a');
	}
	event.stop(event);
	new Ajax.Request(element.href+'&script=Yes', {method: 'get', onSuccess: setUpMoviePanel});
	
	return false;
}
function setUpMoviePanel(response) {

	Effect.ScrollTo('doc');
	$$('body')[0].insert(response.responseText,{ position: 'bottom' });
	
	var arrayWebViewSize = getWebViewSize();
	$('movie_bg').style.height = arrayWebViewSize[1]+"px";
	
	$$('#movie_player_holder ul a').each(function(value, index) { value.observe('click', changeMovieTab);});
	$$('.panelMovies').each(function(value, index) { value.observe('click', changeMovie);});
	$$('.panelMovies').each(function(value, index) { value.observe('mouseover', changeNameOver);});
	$$('.panelMovies').each(function(value, index) { value.observe('mouseout', changeNameOut);});
	
	$('movie_close').observe('click', closeMoviePanel);
	
	$('movie_name').innerHTML = $$('.thumbs .select')[0].title;
	$$('.panelMovies').each(function(value, index) { value.observe('onmouseover', changeName);});
}
function changeMovieTab(event) {
	$$('#movie_player_holder ul a').each(function(value, index) { value.removeClassName('select');});
	$$('.panelMovies').each(function(value, index) { value.removeClassName('select');});
	var element = event.element();
	if(!element.href){
		element = event.findElement('a');
	}
	event.stop(event);
	
	var section = getURLVarFromString(element.href,'section');
	
	$$('.thumbs').each(function(value, index) { value.hide();});

	$('movieTab'+section).addClassName('select');
	$('thumbs'+section).show();
	element.addClassName('select');
	movie = playVideo(640,360,'file=/sitestyle/movies/flv/'+getURLVarFromString(element.href,'movie')+'.flv&skin=/sitestyle/movies/bekle.swf&controlbar=over','movies');
	$('movie_player').replace('<div id="movie_player">'+movie+'</div>');
	
	$$('#thumbs'+section+' a')[0].addClassName('select');
	return false;
}
function changeNameOver(event) {
	var element = event.element();
	if(!element.href){
		element = event.findElement('a');
	}
	$('movie_name').innerHTML = element.title;
}
function changeNameOut(event) {
	$('movie_name').innerHTML = $$('.thumbs .select')[0].title;
}
function changeMovie(event) {
	$$('.panelMovies').each(function(value, index) { value.removeClassName('select');});
	var element = event.element();
	if(!element.href){
		element = event.findElement('a');
	}
	event.stop(event);
	element.addClassName('select');
	movie = playVideo(640,360,'file=/sitestyle/movies/flv/'+getURLVarFromString(element.href,'movie')+'.flv&skin=/sitestyle/movies/bekle.swf&controlbar=over','movies');
	$('movie_player').replace('<div id="movie_player">'+movie+'</div>');
		
	return false;
}

function closeMoviePanel(event) {
	event.stop(event);
	$('movie_bg').remove();
	$('moviepanelholder').remove();
}

function getURLVarFromString(string,urlVarName) {
	//divide the URL in half at the '?' 
	var urlHalves = String(string).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		//load all the name/value pairs into an array 
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable 
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array 
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable 
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}
