/**
 * JS for mobile info page.
 */

function mobileInfo() {

	// P-e-r O-l-a, börja klippa här
	this.activePictureBtn	=		null;
	this.mobilePicObj		=		null;
	this.picturesBoxes		=		null;
	this.nrofpictures		=		null;
	this.pictureListText	=		null;
	// P-e-r O-l-a, sluta klippa här
	
	this.NoSub = null;
	
	this.init = function() {
	
		// P-e-r O-l-a, börja klippa här
		if( $('mobilePic'))
			this.mobilePicObj = $('mobilePic');
		
		if( $('picturesBoxes'))
		{
			this.picturesBoxes = $('picturesBoxes');
			this.nrofpictures = this.picturesBoxes.getElementsByTagName("img").length;
		}
		
		

		if($('list-text'))
			this.pictureListText = $('list-text');
		// P-e-r O-l-a, sluta klippa här
		
		this.mobile_list = $('mobileInfo').getElementsByTagName('div');
		
		if(this.NoSub)
		{
			this.setDefaultModelColor($('select_colors_nosub'));
			
		}
		
		else
		{
			for(var i = 0;i < this.mobile_list.length;i++) {
				if(this.mobile_list[i].className == 'colors')
					this.setDefaultModelColor(this.mobile_list[i]);
			}
		}		
	}
	
	
	
	this.setModelColor = function(obj, mobile_image_src, in_stock, deliverytext, deliveryclass) {
	
		

		this.radiobtn = obj.parentNode.getElementsByTagName('input')[0];
		this.radiobtn.checked = true;
		
		if(this.NoSub)
		{			
			if(mobile_image_src != 'none') {
				this.mobile_img = $('infoImageImg');
				this.mobile_img.src = mobile_image_src;
			}			
		}
		
		else
		{	
			if(mobile_image_src != 'none') {
				this.mobile_img = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('img')[0];
				this.mobile_img.src = mobile_image_src;
			}
		}
			
		this.img_arr = obj.parentNode.parentNode.getElementsByTagName('img');
		
		// Revert all colors to default
		for(var i = 0;i < this.img_arr.length; i++) {
			if(this.img_arr[i].className == 'color') {				
				this.img_arr[i].src = this.img_arr[i].parentNode.getElementsByTagName('input')[2].value;
			}
			
			if(this.img_arr[i].className == 'arrow') 
				this.img_arr[i].style.visibility = 'hidden';
		}
		
		if(in_stock != null)
		{
			
			
			if(in_stock == "True")
			{
				
				
					//chooseDivPostNoSub hanterar även post med sub - trots namnet..
					//$('chooseDivPost').style.display = "block";
					$('chooseDivPostNoSub').style.display = "block";
					$('chooseDivPre').style.display = "block";
					
					if(deliveryclass != null)
						$('nosubdeliverystatus').className = deliveryclass;					
					else
						$('nosubdeliverystatus').className = "instock";					
					
					$('nosubdeliverystatus').innerHTML = deliverytext;
					
					//postpaid sub valt 
					//Är borttagen...
					/*
					if(deliveryclass != null)
						$('deliverystatuspost').className = deliveryclass;					
					else
						$('deliverystatuspost').className = "instock";				
					$('deliverystatuspost').innerHTML = deliverytext;
					*/
					
					
					//prepaid
					if(deliveryclass != null)
						$('deliverystatus').className = deliveryclass;					
					else
						$('deliverystatus').className = "instock";	
						
					
					$('deliverystatus').innerHTML = deliverytext;
					
					
					
			
				//se ovan i denna metod
				/*$('outidpost').style.display = "none";*/
				$('outidpostnosub').style.display = "none";
				$('outidpre').style.display = "none";
				
			}
			
			else
			{
		
				
				

				//$('chooseDivPost').style.display = "none";
				$('chooseDivPostNoSub').style.display = "none";
				$('chooseDivPre').style.display = "none";
				
				
				$('nosubdeliverystatus').className = "outofstock";
				$('nosubdeliverystatus').innerHTML = deliverytext;
				
				
			//	if($('deliverystatuspost').className != "outofstock")
			//		$('deliverystatuspost').className = "outofstock";				
				
			//	if($('deliverystatuspost').innerHTML != deliverytext)				
			//		$('deliverystatuspost').innerHTML = deliverytext;
					
					
				if($('deliverystatus').className != "outofstock")
					$('deliverystatus').className = "outofstock";				
				
				if($('deliverystatus').innerHTML != deliverytext)				
					$('deliverystatus').innerHTML = deliverytext;
				
				
				
			//	$('outidpost').style.display = "block";
				$('outidpostnosub').style.display = "block";
				$('outidpre').style.display = "block";
				
				
				
				
			
			}
			/*
		
			for(var i = 0; i<tjosan.length;i++)
			{
				if(tjosan[i].className == 'orange_button_big')
				{
					if(in_stock == "True")
						tjosan[i].style.display = "block";
					else
						tjosan[i].style.display = "none";
				}
				
				if(tjosan[i].id = "outid")
				{
					if(in_stock == "True")
						tjosan[i].style.display = "none";
					else
						tjosan[i].style.display = "block";
				
				}
				
				
				
				
				
			}
			*/
		}
		
		// and set the new model color
		
		this.active_color = obj.parentNode.getElementsByTagName('img')[1];
		this.active_color.src = this.active_color.parentNode.getElementsByTagName('input')[1].value;
		
		if(this.arrow)
			this.arrow.src = "/images/shop/arrow_black_hidden.gif";			
		
		this.arrow = obj.parentNode.getElementsByTagName('img')[0];
		this.arrow.style.visibility = 'visible';
		
		this.arrow.src = "/images/shop/arrow_black.gif";
				
	}
	
	this.setDefaultModelColor = function(colorBarObj) {
	
		
		this.input_arr = colorBarObj.getElementsByTagName('input');
		
		
		for(y = 0;y < this.input_arr.length; y++) {
			if(this.input_arr[y].checked) {
				this.setModelColor(this.input_arr[y].parentNode.getElementsByTagName('img')[1], 'none', null);
			}
		}
	}
	
	// P-e-r O-l-a, börja klippa här
	this.changeMobilePicture = function(btnObj, pictureSrc, index)
	{
		if(this.pictureListText)
		{
			this.pictureListText.innerHTML = "Bild " + index + " av " + this.nrofpictures;
			
		if(this.activePictureBtn)
		{
			
			this.activePictureBtn.src = "/images/shop/del2/btn_listbox.gif"
			this.activePictureBtn = btnObj;
		}
		else
		{
			$('swimg1').src = "/images/shop/del2/btn_listbox.gif";
			this.activePictureBtn = btnObj;
		}

		this.activePictureBtn.src = "/images/shop/del2/btn_listbox_selected.gif";
		
		}
		
		if(this.mobilePicObj)
			this.mobilePicObj.src = pictureSrc;
	}
	// P-e-r O-l-a, sluta klippa här
	
	this.getMobilePictureCount = function()
	{
		return this.nrofpictures;
	}
}

function customForm() {

	this.activeList = null;
	
	this.activeSubList = null;
	
	this.allowBodyClick = true;
	this.openSelect = function(obj) {
		this.allowBodyClick = false;
		this.list = obj.parentNode.getElementsByTagName('div')[1];
		
		if(this.activeList != this.list && this.activeList != null)
			this.closeSelect(this.activeList);	
		
		var IsEqual = true;
		if(this.activeList == this.list)
			IsEqual = true;
		
		if(IsEqual && this.list.style.display == 'block')
			this.closeSelect(this.list);
		else
			this.list.style.display = 'block';
		
		this.activeList = this.list;
		
		setTimeout("CustomForm.freeBodyClick()",5);
	}

	this.closeSelect = function(obj) {
		//CustomForm.selectSub('', arg2, arg3, arg4, arg5, false);
		obj.style.display = 'none';
	}
	
	this.freeBodyClick = function() {
		this.allowBodyClick = true;
	}
	
	this.setBoundValue = function(obj,bound_time,price_per_month,start_fee) {
	
		
		
		this.closeSelect(obj.parentNode);
		
		this.contract = obj.parentNode.getElementsByTagName('input')[0];
		this.contract.value = bound_time;
		
		this.bound_time = obj.parentNode.parentNode.parentNode.getElementsByTagName('span')[0];
		this.priceDIV = obj.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('div')[0];
		
		this.price_per_month = this.priceDIV.getElementsByTagName('span')[1];
		this.start_fee = this.priceDIV.getElementsByTagName('span')[0];
		
		this.bound_time.innerHTML = bound_time;
		this.price_per_month.innerHTML = price_per_month;
		
		this.start_fee.innerHTML = start_fee;
		
	}
	
	//VÄLJKNAPPPROBLEMET I IE
//	var selectSubCalled = false;
//	var arg2;
//	var arg3;
//	var arg4;
//	var arg5;
	//SLUT VÄLJKNAPPPROBLEMET I IE
	
	this.selectSub = function(sub, html, mobilemodel, mobilecolor, subpageid, truecall) {
		
//		if(subpageid)//VÄLJKNAPPPROBLEMET I IE
//		{
		
				//VÄLJKNAPPPROBLEMET I IE
		//		selectSubCalled = truecall;
		//		
		//		arg2 = html;
		//		arg3 = mobilemodel;
		//		arg4 = mobilecolor;
		//		arg5 = subpageid;
				//SLUT VÄLJKNAPPPROBLEMET I IE		
			
			$('selectedsub').value = subpageid;			
			
			$('selectedsub').setAttribute('pageid', subpageid);			
			
			$('selecteditem').innerHTML = html;		
			
			
			
			
			if($('boundtime_last_sub') != null && $('boundtime_last_sub').value != null && $('boundtime_last_sub').value != "" && $($('boundtime_last_sub').value) != null) 
				$($('boundtime_last_sub').value).style.display = "none";
			if($('boundtimeChooser' + sub) != null)
				$('boundtimeChooser' + sub).style.display = "block";
			$('boundtime_last_sub').value = 'boundtimeChooser' + sub;
			
			chooseSubMobilePage(mobilemodel, mobilecolor);
//		}//VÄLJKNAPPPROBLEMET I IE
		
		
	}
	
	this.openSelectSub = function(obj)
	{	
		
		
		this.allowBodyClick = false;
		this.list = obj.parentNode.getElementsByTagName('div')[1];		
		
		
		if(this.activeSubList != this.list && this.activeSubList != null)
			this.closeSelect(this.activeSubList);	
		
		var IsEqual = true;
		if(this.activeSubList == this.list)
			IsEqual = true;
		
		if(IsEqual && this.list.style.display == 'block')
		{			
			//CustomForm.selectSub('', arg2, arg3, arg4, arg5, false);
			this.closeSelect(this.list);
		}
		else
			this.list.style.display = 'block';
			
		
		
		
		
		this.activeSubList = this.list;
		
		setTimeout("CustomForm.freeBodyClick()",5);
	}
	
	
	this.changeBoundValue = function(obj,bound_time) {
	
		
		this.contract = obj.parentNode.getElementsByTagName('input')[0];
		this.contract.value = bound_time;
		
		this.bound_time = obj.parentNode.parentNode.getElementsByTagName('span')[0];		
		this.bound_time.innerHTML = bound_time;
	}
	
	this.bodyClick = function() {
		var body = document.getElementsByTagName('body')[0];
		
		$('boundtime_last_sub').value = $('boundtime_default_sub').value;
		
		
		body.onclick = function() {
		
			//somelist = false;//VÄLJKNAPPPROBLEMET I IE
		
			if(CustomForm.activeList != null && CustomForm.allowBodyClick)
			{				
				 CustomForm.activeList.style.display = 'none';
				 CustomForm.activeList = null;
			//	 somelist = true;//VÄLJKNAPPPROBLEMET I IE
			}
				 
			if(CustomForm.activeSubList != null && CustomForm.allowBodyClick)
			{			
				 CustomForm.activeSubList.style.display = 'none';
				 CustomForm.activeSubList = null;
			//	 somelist = true;//VÄLJKNAPPPROBLEMET I IE
			}
			
			
				 
			
			
			//VÄLJKNAPPPROBLEMET I IE
			
			//if(somelist && !selectSubCalled)
			//{	
				
			//	CustomForm.selectSub('', arg2, arg3, arg4, arg5, false);
			//}
			
			//SLUT VÄLJKNAPPPROBLEMET I IE
				
		}
	}	
	
}


function mobileMenu() {
	
	this.ff = 10; // fast forward
	this.f = 4; // forward
	this.b = -4; //backward
	this.fb = -10; // fast backward
	
	this.scrolltimer;
	this.loop = true;
	this.active_scroll = true;
	
	this.init = function() {
		this.menu = $('mobileMenu');
		
		this.menuCtrl1 = $('ctrl1');
		this.menuCtrl2 = $('ctrl2');
		
		this.menuCtrl2_activeSrc = '../images/shop/mobilemenu_arrow_pl_l.gif';
		this.menuCtrl2_disabledSrc = '../images/shop/mobilemenu_arrow_pl_a.gif';
		
		this.menuCtrl1_activeSrc = '../images/shop/mobilemenu_arrow_pr_l.gif';
		this.menuCtrl1_disabledSrc = '../images/shop/mobilemenu_arrow_pr_a.gif';
		
		this.current_arrow =  $('current_arrow');
		
		this.menu_items = this.menu.getElementsByTagName('div')[0].getElementsByTagName('img');
		
		this.menu_container = this.menu.getElementsByTagName('div')[0];
		this.last_menu_item = null;
		
		for(var i = 0;i<this.menu_items.length;i++) {
			
			if(i == (this.menu_items.length-1)) { // this is the last item in the list. Measure from here!
				this.menu_container.style.width = (this.menu_items[i].offsetLeft + 80) + 'px';
				if(this.menu_items[i].offsetLeft < (this.menu.offsetWidth)) {
					this.menuCtrl1.src = this.menuCtrl1_disabledSrc;
					this.menuCtrl2.src = this.menuCtrl2_disabledSrc;
					this.active_scroll = false;
					
				}
			}
			
			if(this.menu_items[i].className == 'current') {
				var x = this.menu_items[i].offsetLeft;
				this.current_arrow.style.left = (x+(this.menu_items[i].offsetWidth/2)-18) + "px";
				
				if(x > this.menu.offsetWidth){
					this.menu.scrollLeft = (x-(this.menu.offsetWidth/2));
				} else {
					this.menuCtrl1.src = this.menuCtrl1_disabledSrc;
				}
				
				//this.setCtrlButton();
			}	
			
		}
	}
	
	this.setCtrlButton = function() {
		if(this.menu.scrollLeft < 5) {
			this.menuCtrl1.src = this.menuCtrl1_disabledSrc;
		} else 
			this.menuCtrl1.src = this.menuCtrl1_activeSrc;
		
		if(this.menu.scrollLeft > (this.menu.getElementsByTagName('div')[0].offsetWidth - this.menu.offsetWidth -10)){
			this.menuCtrl2.src = this.menuCtrl2_disabledSrc;
		} else 
			this.menuCtrl2.src = this.menuCtrl2_activeSrc;
	}
	
	this.StopScroll = function() {
		this.loop = false;
		this.StartScroll(10,false)
		clearTimeout(this.scrolltimer);
	}
	
	this.StartScroll = function(speed,loopRunning) {
		if(this.active_scroll) {
			this.setCtrlButton();
			clearTimeout(this.scrolltimer);
			this.loop = loopRunning;
			if(this.loop)
			{
				if(this.menu.scrollLeft > -1)
				{
					this.menu.scrollLeft += speed;
					this.scrolltimer = setTimeout('MobileMenu.StartScroll('+speed+',true)',10); // scrolls every 10 miliseconds	
				}
			}
		}
	}
	
}


var MobileInfo =  new mobileInfo();
addEvent(window, "load", function(){MobileInfo.init();})

var CustomForm =  new customForm();
addEvent(window, "load", function(){CustomForm.bodyClick();})

var MobileMenu = new mobileMenu();
addEvent(window, "load", function(){MobileMenu.init();})


