/*
 * ConciseCMS
 * version 2.0
 * Copyright(c) , Concise Media Systems O?
 * info@concise.ee
 * 
 * http://www.concise.ee/license/
 */
var conciseCms={};
$(document).ready(function(){
	conciseCms=function(){
		var url=document.location.protocol+'//'+document.location.hostname+'/';
		var menuName=document.location.pathname.split("/")[1];
		var timeout=5000;
		var ajaxReq;
//***
		function findFast(){
			var u=url+''+menuName+'/0/submit/';
			$("#tblContainerCnt").html('<div class="loading"></div>');
			if(ajaxReq){ajaxReq.abort();}
			ajaxReq=$.ajax({
				url:u,
  	  	beforeSend:function(x){if(x && x.overrideMimeType) {x.overrideMimeType("application/j-son;charset=UTF-8");}},
    		type: 'GET',cache:false,data:"{}",dataType:"json",timeout:timeout,
	    	error:function(r){alert('Proovi uuesti');},
  	  	success:function(res){
  	  		var r=eval(res);
  	  		var txt='';
  	  		if(r.length==0){
  	  			txt+='<div class="error">P&auml;ringule vastavaid tulemusi ei leitud!</div>';
  	  		}else{
	  	  		for (var i in r){
	  	  			txt+='<div class="tblCode">'+rawurldecode(r[i].code)+'</div><div class="tblProd"><a href="'+rawurldecode(r[i].url)+'">'+rawurldecode(r[i].title)+'</a></div><div class="tblYear">'+rawurldecode(r[i].year)+'</div><div class="tblPrice tootehind rightalign">'+rawurldecode(r[i].price)+'</div><div class="pics">';
	  	  			if(r[i].p1){
	  	  				txt+='<a rel="fancyBox" href="'+rawurldecode(r[i].p1)+'" title="'+rawurldecode(r[i].code)+'"><img src="'+rawurldecode(r[i].p1)+'" height="20" alt="'+rawurldecode(r[i].code)+'" /></a>';
	  	  			}
	  	  			if(r[i].p2){
	  	  				txt+='<a rel="fancyBox" href="'+rawurldecode(r[i].p2)+'" title="'+rawurldecode(r[i].code)+'"><img src="'+rawurldecode(r[i].p2)+'" height="20" alt="'+rawurldecode(r[i].code)+'" /></a>';
	  	  			}
	  	  			if(r[i].p3){
	  	  				txt+='<a rel="fancyBox" href="'+rawurldecode(r[i].p3)+'" title="'+rawurldecode(r[i].code)+'"><img src="'+rawurldecode(r[i].p3)+'" height="20" alt="'+rawurldecode(r[i].code)+'" /></a>';
	  	  			}
	  	  			txt+='</div>';
  	  			}
  	  		}
  	  		$("#tblContainerCnt").html(txt);
  	  		$("a[rel=fancyBox]").fancybox({
		'transitionIn':'none',
		'transitionOut':'none',
		'titlePosition':'over',
		'titleFormat':function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"><span class="left">' + (title.length ? '' + title : '') + '</span><span class="right">' + (currentIndex + 1) + ' / ' + currentArray.length + '</span></span>';
		}
	});
				}
				
			});
		}
//***
		function changeMark(){
			deleteOptions('car_model_id');
			deleteOptions('car_modification_id');
			getModel();
		}
//***
		function getModel(){
			var mark_id=$("#car_mark_id :selected").val();
			var u=url+''+menuName+'/0/mark/'+mark_id+'/';
			//if(ajaxReq){ajaxReq.abort();}
			ajaxReq=$.ajax({
				url:u,
  	  	beforeSend:function(x){if(x && x.overrideMimeType) {x.overrideMimeType("application/j-son;charset=UTF-8");}},
    		type: 'GET',cache:false,data:"{}",dataType:"json",timeout:timeout,
	    	error:function(r){alert('Proovi uuesti');},
  	  	success:function(res){
  	  		var r=eval(res);
  	  		for (var i in r){
  	  			$("#car_model_id").append('<option value="'+r[i].id+'">'+r[i].title+'</option>');
  	  		}
  	  		findFast();
				}
			});
		}
//***
		function changeModel(){
			deleteOptions('car_modification_id');
			getModification();
		}
//***		
		function getModification(){
			var model_id=$("#car_model_id :selected").val();
			var u=url+''+menuName+'/0/model/'+model_id+'/';
			//if(ajaxReq){ajaxReq.abort();}
			ajaxReq=$.ajax({
				url:u,
  	  	beforeSend:function(x){if(x && x.overrideMimeType) {x.overrideMimeType("application/j-son;charset=UTF-8");}},
    		type: 'GET',cache:false,data:"{}",dataType:"json",timeout:timeout,
	    	error:function(r){alert('Proovi uuesti');},
  	  	success:function(res){
  	  		var r=eval(res);
  	  		for (var i in r){
  	  			$("#car_modification_id").append('<option value="'+r[i].id+'">'+r[i].title+'</option>');
  	  		}
  	  		findFast();
				}
			});
		}
//***
		function changeModification(){
			var modification_id=$("#car_modification_id :selected").val();
			var u=url+''+menuName+'/0/modification/'+modification_id+'/';
			//if(ajaxReq){ajaxReq.abort();}
			ajaxReq=$.ajax({
				url:u,
  	  	beforeSend:function(x){if(x && x.overrideMimeType) {x.overrideMimeType("application/j-son;charset=UTF-8");}},
    		type: 'GET',cache:false,data:"{}",dataType:"json",timeout:timeout,
	    	error:function(r){alert('Proovi uuesti');},
  	  	success:function(res){
  	  		findFast();
				}
			});
		}
//***
function changeGlassType(){
			var glass_type_id=$("#glass_type_id :selected").val();
			var u=url+''+menuName+'/0/glass/'+glass_type_id+'/';
			//if(ajaxReq){ajaxReq.abort();}
			ajaxReq=$.ajax({
				url:u,
  	  	beforeSend:function(x){if(x && x.overrideMimeType) {x.overrideMimeType("application/j-son;charset=UTF-8");}},
    		type: 'GET',cache:false,data:"{}",dataType:"json",timeout:timeout,
	    	error:function(r){alert('Proovi uuesti');},
  	  	success:function(res){
					findFast();
				}
			});
		}
//***
		function deleteOptions(i){
			$("#"+i).children().remove();
			$("#"+i).append('<option id="0">-</option>');
		}
//***
		function showDiv(v,s){
			if($("#"+v).attr('checked')==true){
				$("#"+s).css('display','block');
			}else{
				$("#"+s).css('display','none');
			}
		}
//***
		function submitForm(i){$("#"+i).submit();}
//***
		function msgBox(msg){//private
			$("#message_box").css('display','block');
			$("#message_box").css('opacity','100');
			$("#message_box #msg").html(msg);
			$('#message_box').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350});  
			$(window).scroll(function(){$('#message_box').animate({top:$(window).scrollTop()+"px"},{queue:false,duration:350});});
			//when the close button at right corner of the message box is clicked 
			setTimeout(function(){$('#message_box').animate({ top:"+=15px",opacity:0},"slow");},2000);
			$('#close_message').click(function(){$('#message_box').animate({ top:"+=15px",opacity:0},"slow");});
		}
//***
		function ajaxResponse(r){return decodeURIComponent(r);}
//***
		function ajaxRequest(r){return encodeURIComponent(r);}
//***
		function rawurldecode(url){
			var chr, a, len, ret, c, c2, c3, c4, hi, low;
			ret='';
			for(a=0,len=url.length;a<len;a++){
				chr=url.charAt(a);
				if(chr!='%'){
					ret+=chr;
					continue;
				}
				c=parseInt(url.charAt(a+1) + url.charAt(a+2), 16);
				if(isNaN(c)){
					ret += '%';
					continue;
				}
				a += 2;
				ret += String.fromCharCode(c);
			}
			url = ret;
			ret = '';
			for (a = 0, len = url.length; a < len; a++){
				c = url.charCodeAt(a);
				if(      (c &      0x80) === 0        ){
					ret += url.charAt(a);
				}
				else if ((c &      0xE0) ===      0xC0){
					a++;
					c2 = url.charCodeAt(a);
					ret += String.fromCharCode(
							((c  & 0x1F) << 6) | 
							((c2 & 0x3F) << 0)
						);
				}
				else if ((c &      0xF0) ===      0xE0){
					a++;
					c2 = url.charCodeAt(a);
					a++;
					c3 = url.charCodeAt(a);
					ret += String.fromCharCode(
						       ((c  & 0x0F) << 12) |
						       ((c2 & 0x3F) << 6 ) |
				    		   ((c3 & 0x3F) << 0 )
					       );
				}
				else if ((c &      0xF8) ===      0xF0){
					a++;
					c2 = url.charCodeAt(a);
					a++;
					c3 = url.charCodeAt(a);
					a++;
					c4 = url.charCodeAt(a);
					c =	((c  & 0x07) << 18) |
						((c2 & 0x3F) << 12) |
						((c3 & 0x3F) << 6 ) |
						((c4 & 0x3F) << 0 ) ;
					if (c >= 0x10000){
						c -= 0x10000;
						hi  = (c & 0xFFC00) >> 10; // first 10 bits
						low = c & 0x003FF; // last  10 bits
						hi  += 0xD800; // high surrogate range
						low += 0xDC00; // low surrogate range
						ret += String.fromCharCode(hi, low);
					}else{
						ret += String.fromCharCode(c);
					}
				}
			}
			return ret;
		}
//***
		return {
			changeMark:function(){changeMark();},
			changeModel:function(){changeModel();},
			changeModification:function(){changeModification();},
			changeGlassType:function(){changeGlassType();},
			findFast:function(){findFast();},
			showDiv:function(v,s){showDiv(v,s);},
			submitForm:function(i){submitForm(i);}
		}
	}();
});












function printPage(){
	if (window.print)
  	window.print()
  else
		alert("Your browser probably does't support Javascript");
}
var windowNote;
function showPic(title, pic, colorBack, wid, hei, idee)
{
	var idee = idee|| "default";
	windowNote = window.open('','Note'+ idee,'top=5, left=5, toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width=' + wid + ',height='+ hei);
	image = '<a href="javascript:window.close()"><img src="'+ pic +'" width="'+ wid +'" height="'+ hei +'" border="0" alt="X"></a>';
	text = "<html><head><META HTTP-EQUIV=\"imagetoolbar\" CONTENT=\"no\"><title>"+ title + "</title></head><body bgcolor=\"#" + colorBack +"\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">" + image + "</body></html>";
	windowNote.document.write(text);
	windowNote.focus();
	windowNote.document.close();
	return false;
}
var type = "IE";
BrowserSniffer();
function BrowserSniffer()
{
	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="OP";	//Opera
	else if (document.all) type="IE";							//Internet Explorer e.g. IE4 upwards
	else if (document.layers) type="NN";							//Netscape Communicator 4
	else if (!document.all && document.getElementById) type="MO";				//Mozila e.g. Netscape 6 upwards
	else type = "IE";									//I assume it will not get here
}
function hide_div(div_name)
{
	if (type=="IE") eval("document.all."+div_name+".style.display='none'");
	if (type=="NN") eval("document."+div_name+".display='none'");
	if (type=="MO" || type=="OP") eval("document.getElementById(div_name).style.display='none'");
}
function showhide_div(div_name)
{
	if (type=="IE")
	{
		if(eval("document.all."+div_name+".style.display")!='none')
		{
			eval("document.all."+div_name+".style.display='none'");
		}
		else
		{
			eval("document.all."+div_name+".style.display='block'");
		}
	}
	if (type=="NN")
	{
		if(eval("document."+div_name+".display")!='none')
		{
			eval("document."+div_name+".display='none'");
		}
		else
		{
			eval("document."+div_name+".display='block'");
		}
	}
	if (type=="MO" || type=="OP")
	{
		if(eval("document.getElementById(div_name).style.display")!='none')
		{
			eval("document.getElementById(div_name).style.display='none'");
		}
		else
		{
			eval("document.getElementById(div_name).style.display='block'");
		}
	}
}
function submit_form()
{
	document.getElementById('vorm').submit();
}
function reset_form()
{
	document.getElementById('vorm').reset();
}
function change_picture(id){
	document.getElementById('mainpic').src=id;
}



