/* That script stretches the height size of the <body> tag to 100% if its first size is smaller than the screen height. */

	$(document).ready(function() {
		$('div.tabs ul li.tab_0').click(function(){
			if ($(this).not('.active')){
				$('div#tab_content_1').fadeOut('fast');
				$('div#tab_content_2').fadeOut('fast');
				$('div#tab_content_0').fadeIn('fast');
			};
			$('div.tabs ul li').removeClass('active');
			$(this).addClass('active');

		});

		$('div.tabs ul li.tab_1').click(function(){
			if ($(this).not('.active')){
				$('div#tab_content_0').fadeOut('fast');
				$('div#tab_content_2').fadeOut('fast');
				$('div#tab_content_1').show('fast');
			};
			$('div.tabs ul li').removeClass('active');
			$(this).addClass('active');
		});

		$('div.tabs ul li.tab_2').click(function(){
			if ($(this).not('.active')){
				$('div#tab_content_0').fadeOut('fast');
				$('div#tab_content_1').fadeOut('fast');
				$('div#tab_content_2').fadeIn('fast');
			};
			$('div.tabs ul li').removeClass('active');
			$(this).addClass('active');
		});

	});

	//
	var last_model = "";
	var interval = "";
	function observeUrl()
	{
		//alert(last_model);
		var current_model = window.location.hash.substring(1);

	//	if(last_model == current_model && current_model=='') document.location='/';

		if(last_model == current_model && current_model!='') return;
		else
		{
			//clearInterval(interval);
			d = new Date();
			get_data(current_model,d.getTime());
			last_model = current_model;
		}
	}

	// change the image regarding the clicked small thumb
	function get_data(name,time)
     {
     	var p = {};
 		p['model_name'] = name;
 		p['avoid_cash'] = time;

		$.ajax({
		  type: 'post',
		  url: 'collection_model/index',
		  dataType: 'json',
		  data: p,
		  success: function(data){
			document.title = "O-connect " + data.phone_name;
		  	$('.buy, .gallery_content, .tabs_content').fadeOut('slow', function(){

		  			$('#content_place_holder .gallery_content img').attr("src","/uploads/phones/main_collection_" + data.phone_id + ".jpg");
			  		$('.buy .name strong').html(data.phone_name);
				  	$('.buy .price p strong').html(data.price + " BGN");
				  	$('#left_spec_column').html(data.left_spec_column);
				  	$('#right_spec_column').html(data.right_spec_column);
			  });
			  $('.buy, .gallery_content, .tabs_content').fadeIn('slow');

			  last_model = name;
			  interval = setInterval(observeUrl,'1000');
		  },
		  error: function(xhr, ajaxOptions, thrownError){if(xhr.status) document.location = '/';}
		});

     }

	// create a div on the fly to be used for the order form
	var maskpanel=function()
	{
		this.divobj;
		this.show=function()
			{
				if(!document.getElementById("xdivmasking"))
				{
				  var divEle=document.createElement('div');
				   divEle.setAttribute("id","xdivmasking");
				   document.body.appendChild(divEle);

				   var divSty=document.getElementById("xdivmasking").style;
				   divSty.position="absolute"; divSty.top="0px"; divSty.left="0px";
				}

				this.divobj=document.getElementById("xdivmasking");

				var dsh=document.documentElement.scrollHeight;
				var dch=document.documentElement.clientHeight;
				var dsw=document.documentElement.scrollWidth;
				var dcw=document.documentElement.clientWidth;

				var bdh=(dsh>dch)?dsh:dch;
				var bdw=(dsw>dcw)?dsw:dcw;

				this.divobj.style.height=bdh+'px';
				this.divobj.style.width=bdw+'px';
				this.divobj.style.display="block";

			};
		this.hide=function()
		{
		   //this.waitifrm.style.display=
		   this.divobj.style.display="none";
		};
	}

	/**
	 *
	 * @access public
	 * @return void
	 **/
	function ShowMask(product,price){
		 divmask=new maskpanel();
		 var formData = {};
		 formData['product'] = product;
		 formData['price'] = price;

		 divmask.show()
		 $.ajax({
			type: 'post',
			url: '/orders/form_buy',
			data: formData,
			success: function(data){
						$('#xdivmasking').html(data);
						$('#xdivmasking div').fadeIn('slow');
					}

		 });
	}

	/**
	 *
	 * @access public
	 * @return void
	 **/
	function buy(product){
		var formData = {};
		formData['txtEmail'] = document.getElementById('txtEmail').value;
		formData['txtName'] = document.getElementById('txtName').value;
		formData['txtComments'] = document.getElementById('txtComments').value;
		formData['txtPhone'] = document.getElementById('txtPhone').value;
		formData['product'] = product;

		$.ajax({
		type: "POST",
		url: '/orders/make_order',
		data: formData,
		dataType: 'json',
		success: function(data) {
			$('#dynamic').css({color:data.color});
			$('#dynamic i').html(data.message)
			.hide()
			.fadeIn(1500, function() {});
			//alert(data.message);
				if(data.success == 1)
				{
					document.getElementById('txtEmail').value = '';
					document.getElementById('txtName').value = '';
					document.getElementById('txtComments').value = '';
					document.getElementById('txtPhone').value = '';
					setTimeout(function(){
						$('#xdivmasking div').fadeOut('slow',function(){
							$('#xdivmasking').hide();
						});
					},3000);
				}
			}
		 });

	}

	/**
	 *
	 * @access public
	 * @return void
	 **/
	function HideMask(){

		$('#xdivmasking div').fadeOut('slow',function(){
				$('#xdivmasking').hide();
		});
	}

