// JavaScript Document

var userLang = (navigator.language) ? navigator.language : navigator.userLanguage; 
var _default = userLang.substring(0,2);
var lang, str;

//$(document).ready(function(){
    $.translate.ready(function(){
   		//lang = $.translate.getLanguages();
   		lang = $.translate.getLanguages(true,['da','en','fi','fr','el','it','ko','no','pl','pt','ru','es','sv','tr']);
   		var node = document.getElementById('listing');
   		var _sel;
   		
   		if(Get_Cookie("_lang")) {
    		var _l = Get_Cookie("_lang");
    		change_lang(_l);
    	}else {
    		change_lang(_default);
    	}
    	
   		str = '<select name="langs" id="langs">';
   		for (var i in lang)
   		{
   			_sel = '';
   			if(i.toLowerCase() == _l) {
   				_sel = 'selected="selected"';
   			}
   			
   			str += '<option id="" value="'+i.toLowerCase()+'" '+_sel+'>'+i.charAt(0).toUpperCase()+i.substr(1).toLowerCase()+'</option>';
   		}
   			str += '</select>';
   		
   		if(node) {
   			node.innerHTML = str;
   		}
   		
   		$('#langs').change(function() {
   			document.cookie = "_lang="+$(this).val();
   			change_lang($(this).val());
    		
    	});

    	function change_lang(_l) {
    		$('.translate').translate(_l);
    		$('#dSubmenu').translate(_l);
    		$('form#bake_form').translate(_l);
    		/*$('#rightCol').translate(_l);*/
    	}
    });
//});

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


var activeItem1;

var activeItem2;

function toggleImages(id){

	if(activeItem2){

		activeItem1.style.visibility = "hidden";

		activeItem2.style.visibility = "hidden";

	}

	activeItem1 = document.getElementById(id);

	activeItem2 = document.getElementById(id);

	if(activeItem1){

		activeItem1.style.visibility = "visible";

	}

	if(activeItem2){

		activeItem2.style.visibility = "visible";

	}

}



function MM_showHideLayers() { //v9.0

  var i,p,v,obj,args=MM_showHideLayers.arguments;

  for (i=0; i<(args.length-2); i+=3) 

  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];

    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }

    obj.visibility=v; }

}

function submitForm(form_id){

	form_id.submit();

	return false;

}

function clearForm(form_id){

	form_id.reset(); 

	return false;

}

function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);

}

function printThis(id){

	top.printFrame.focus();

	top.printFrame.print();

}

function checkForm(form_id){

	var counter = 0;

	var f_items = [document.forms[0].name_id,

	document.forms[0].phone_id,

	document.forms[0].adress_id,

	document.forms[0].postno_id,

	document.forms[0].company_id,

	document.forms[0].email_id,

	document.forms[0].country_id];

	for (var i=0;i<f_items.length;i++){

		if(f_items[i].value == ""){

			f_items[i].style.borderColor = "#FF0000";

		}else{

			counter++;

			f_items[i].style.borderColor = "#b9b49e";

		}

	}

	if(counter == 7){

		submitForm(form_id);

	};

}


