/**
 * JS for mobile list page.
 */

function mobileList() {
	
	this.init = function() {
		
		this.viewHeader = $('viewHeader'); // page img header
		
		this.tab1ActiveSrc = "/templates/media/gfx/mobile/mobile_tab1_active.gif";
		this.tab2ActiveSrc = "/templates/media/gfx/mobile/mobile_tab2_active.gif";
		this.tab3ActiveSrc = "../images/shop/mobile_tab3_active.gif";
		this.tab4ActiveSrc = "/templates/media/gfx/mobile/koprad_active.gif";
		this.activeTabLinkSrc = "/templates/media/gfx/mobile/mobile_tab1_inactive.gif";
		this.activeTabObj = $('activeTab');
		
		this.arrViews = new Array();
		if($('view1')) 
			this.arrViews.push($('view1')); 
        if($('view2'))
            this.arrViews.push($('view2'));
        if($('view3'))
            this.arrViews.push($('view3'));
        if($('view4'))
			this.arrViews.push($('view4'));

		
		this.mobile_list = $('mobile_list').getElementsByTagName('div');
		
		for(var i = 0;i < this.mobile_list.length;i++) {
			if(this.mobile_list[i].className == 'colors')
				this.setDefaultModelColor(this.mobile_list[i]);
		}	
		
		this.initialized = true;	
	}
	
	this.setModelColor = function(obj, mobile_image_src, in_stock) {

		this.radiobtn = obj.parentNode.getElementsByTagName('input')[0];
		this.radiobtn.checked = true;
	
		if(mobile_image_src != 'none') {
			this.mobile_img = obj.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].attributes['defaultsrc'].value;
				if(!this.img_arr[i].parentNode.getElementsByTagName('input')[2])
					alert('sdfdsf');

				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)
		{
			
			var tjosan = obj.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('*');
			
			var outofstockimg = obj.parentNode.parentNode.parentNode.getElementsByTagName('*');
			
			var images = obj.parentNode.parentNode.parentNode.getElementsByTagName('img');
			
			var readMoreImage;
			
			for(var i = 0; i<images.length; i++)
			{
				if(images[i].className == 'readmoreNoSubInStock' || images[i].className == 'readmoreNoSub')
				{
					readMoreImage = images[i];
					break;
				}
					
			}
			
			var holder;
			
			if(readMoreImage != null) //Erik
			{
			for(var i = 0; i<outofstockimg.length; i++)
			{
				
				if(outofstockimg[i].className == 'outofstockNoSub')
				{					
					if(outofstockimg[i].style.display == 'none')
					{						
						if(in_stock != "True")
						{							
							outofstockimg[i].style.display = 'block';
							readMoreImage.className = 'readmoreNoSub';							
							break;
						}
					}
					
					else
					{
						if(in_stock == "True")
						{
							outofstockimg[i].style.display = 'none';
							readMoreImage.className = 'readmoreNoSubInStock';							
							break;
						}						
					}			
				}	
			}
			}
			
			
			
			
			
			for(var i = 0; i<tjosan.length;i++)
			{
				if(tjosan[i].className == 'orange_button')
				{
					if(in_stock == "True")
						tjosan[i].style.display = "block";
					else
						tjosan[i].style.display = "none";
				}
				
				
				if(tjosan[i].className == 'not_available')
				{
					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;
		
		this.arrow = obj.parentNode.getElementsByTagName('img')[0];
		this.arrow.style.visibility = 'visible';
				
	}
	
	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) {
			if(y == 0){
				this.setModelColor(this.input_arr[y].parentNode.getElementsByTagName('img')[1], 'none', null);
			}
		}
	}
	
	this.changeMobileView = function(btnObj,view_id, header_src) {

		if(!this.arrViews)
			return;
		
		if(this.viewHeader)
			this.viewHeader.src = header_src;
		
		if(this.activeTabObj != null) {
			this.activeTabObj.src = this.activeTabLinkSrc;
		}
		
		this.activeTabLinkSrc = btnObj.src;
		this.activeTabObj = btnObj;
		
		if(view_id == 'view1') 
			btnObj.src = this.tab1ActiveSrc;
		else if(view_id == 'view2')
			btnObj.src = this.tab2ActiveSrc;
		else if(view_id == 'view3')
			btnObj.src = this.tab3ActiveSrc;
		else if(view_id == 'view4')
			btnObj.src = this.tab4ActiveSrc;
		
		
		for (i = 0;i<this.arrViews.length;i++) {
			if(this.arrViews[i].id == view_id) {
				this.arrViews[i].style.display = "block";
				
			} else {
				this.arrViews[i].style.display = "none";
				
			}
		}
		if(this.arrViews[0].id == view_id) {
			this.arrViews[0].style.display = "block";
		}
	}
}

function customForm() {

	this.activeList = 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) {
		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.price_per_month = obj.parentNode.parentNode.parentNode.getElementsByTagName('div')[0];
		this.start_fee = obj.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('b')[0];
		
		
		this.bound_time.innerHTML = bound_time;
		
		
		
		
		this.price_per_month.innerHTML = price_per_month;
		
		if(this.start_fee != null)
		{
		if(start_fee != 0) {
			this.start_fee.innerHTML = start_fee;
			this.start_fee.parentNode.style.display = 'block';
		} else {
			this.start_fee.parentNode.style.display = 'none';	
		}
		}
		
		
	}
	
	this.changeBoundValue = function(obj,bound_time) {
	
		this.contract = obj.parentNode.getElementsByTagName('input')[0];
		this.contract.value = bound_time;		
		
		
		
		obj.parentNode.parentNode.parentNode.getElementsByTagName('span')[0].innerHTML = bound_time;
		
		this.bound_time = obj.parentNode.parentNode.parentNode.getElementsByTagName('span')[0];		
	}
	
	this.bodyClick = function() {
		var body = document.getElementsByTagName('body')[0];
		
		
		body.onclick = function() {
			if(CustomForm.activeList != null && CustomForm.allowBodyClick)
				 CustomForm.activeList.style.display = 'none';
		}
	}	
}

var MobileList =  new mobileList();
addEvent(window, "load", function(){MobileList.init();})

var CustomForm =  new customForm();
addEvent(window, "load", function(){CustomForm.bodyClick();})


