﻿$(document).ready(function() {
	$('hr').each(function(){
		var obj = $(this);
		var cssStyle = obj.attr('style');cssStyle=(cssStyle?' style="'+cssStyle+'"':'');
		var cssClass = obj.attr('class');cssClass=' class="hr '+(cssClass?cssClass:'')+'"';
		obj.wrap('<div '+cssClass+cssStyle+' />');
		obj.removeAttr('class');
		obj.removeAttr('style');
	});
	
	$('.round').each(function() {
		var obj = $(this);
		var css = 'margin:' + obj.css('marginTop') + ' ' + obj.css('marginRight') + ' ' + obj.css('marginBottom') + ' ' + obj.css('marginLeft') + ';';
		if (obj.css('float') != 'none1') { css = css + 'float:' + obj.css('float') + ';'; }
		//var n=$(this).css("z-index");n=isNaN(n)?-1:n;
		//css+='z-index:'+n+';'
		$(this).wrap('<span class="round" style="' + css + ';" />');
		$(this).css('margin', '').css('float', '');
	});

	$("select.round,input.round").focus(function(e) {
		$(this).addClass("has-focus");
		$(this).parent('span.round').addClass("round-has-focus");
		$(this).parent('span.round_button').addClass("round_button-has-focus");
	});
	$("select.round,input.round").blur(function() {
		$(this).removeClass("has-focus");
		$(this).parent('span.round').removeClass("round-has-focus");
		$(this).parent('span.round_button').removeClass("round_button-has-focus");
	});

	$("input.round[type=\'submit\'],input.round[type=\'button\'],input.round[type=\'reset\']").each(function() {
		$(this).parent('span.round').addClass('round_button');
	});
	$("input.round[type=\'submit\']").each(function() {
		$(this).parent('span.round').addClass('round_button_mvr');
	});

	$("input.prefill[title]").each(function() {
		if ($(this).val() == '') { $(this).val($(this).attr('title')); }
		else if ($(this).val() != $(this).attr('title')) { $(this).removeClass('prefill') }
		$(this).focus(function() { if ($(this).val() == $(this).attr('title')) { setCaretToPos(this, 0); } });
		$(this).keydown(function() { if ($(this).val() == $(this).attr('title')) { $(this).removeClass('prefill').val(''); } });
		$(this).keyup(function() { if ($(this).val() == '') { $(this).addClass('prefill').val($(this).attr('title')); setCaretToPos(this, 0); } });
		$(this).blur(function() { if ($(this).val() == '') { $(this).addClass('prefill').val($(this).attr('title')); } });
		$(this).closest("form").submit(function(e) {
			$("input.prefill[title]").each(function() {
				if ($(this).val() == $(this).attr('title')) {
					$(this).val('');
				}
			});
		});
	});

	$('a.button').corner({
		tl: { radius: 5 },
		tr: { radius: 5 },
		bl: { radius: 5 },
		br: { radius: 5 },
		antiAlias: true
	});

	$('span.round').corner({
		tl: { radius: 5 },
		tr: { radius: 5 },
		bl: { radius: 5 },
		br: { radius: 5 },
		antiAlias: true
	});

	/*ensure that #body_m is the same height as the <body> tag */
	var docHeight = $(document).height();
	var viewHeight = $(window).height();

	//alert(docHeight);
	//alert(viewHeight);

	if (docHeight > viewHeight) {
		docHeight += 15;
	} else {
		docHeight = docHeight;
	}
	$('#body_m').height(docHeight);
	//alert($('#body_m').height());

	/*update equal height columns*/
	setTimeout("equalHeightStart()", 100);
});

function equalHeightStart(){
	var i=1;
	var l=10;
	for(i;i<=l;i++){
		equalHeight($('.ehg_'+i));
	}
//	group.each(function(){
//		equalHeight($(".equalHeight:not([rel])"));
//		equalHeight($(".equalHeight([rel='"+$(this).attr('rel')+"'])"));
//	});
}
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
	//[rel='gallerybox']
}

function cancelEvent(e){
	if(!e) e = window.event;
	try{
		e.returnValue = false;
		e.cancelBubble = true;
	}catch(err){}
	try{
		//e.cancelBubble is supported by IE - this will kill the bubbling process.
		if(e.returnValue){
			e.returnValue = false;
		}
		if(e.cancelBubble){
			e.cancelBubble = true;
		}
	}catch(err){}
	try{
		//e.stopPropagation works only in Firefox.
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	}catch(err){}
}

//Extending jQuery with a better trigger function
(function($){
    //Attach to jQuery
    $.fn.extend({ 
		triggerLink: function(e, url) {
			if(e.target.tagName.toLowerCase() != 'a'){
				cancelEvent(e);
				if(e.ctrlKey){
					window.open(url,'a'+Math.random());
				}else if(e.shiftKey){
					window.open(url,'_blank','menubar=1,toolbar=1,status=1');
				}else{
					window.location = url;
				}
			}
        }
    });
     
//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )
   })(jQuery);

function hasFocus(jqueryObject){
	return jqueryObject.hasClass('has-focus');
}

function fillDropDown(id,JSONdata,postUpdateFn){
	if(id!=null){
		var jddl = $('#' + id);
		var ddl = jddl[0];
		var url = jddl.attr('data-url');
		var ie7 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 7) ? true : false; 
		
		ddl.options.length=0;
		ddl.options.add(new Option('Henter data...', '-1'));
		ddl.selectedIndex=0;
		//alert(JSONdata);
		$.ajax({
			type: 'GET'
			, url: url + (ie7 == true ? '&ie7=true' : '')
			, data: jQuery.parseJSON(JSONdata)
			, dataType: 'html'
			, async: true
			, success: function(html)
			{
				eval(html);
				if (typeof (JSONres) != "undefined") {
					if (JSONres != null && typeof (JSONres.element) != 'undefined'){
						
						var jObj = $('#'+JSONres.element);
						var ddl = jObj[0];
						var dataDefault = jObj.attr('data-default');
						
						ddl.options.length=0;
						if (typeof (JSONres.items) != 'undefined' && JSONres.items.length > 0) {
							var l = JSONres.items.length;
							var selectedIndex = 0;
							for (var i = 0; i < l; i++) {
								var opt;
								if(JSONres.items[i].text == '-' && JSONres.items[i].value == '-'){
									opt = new Option('------','-');
									opt.disabled=true;
								}else{
									opt = new Option(JSONres.items[i].text, JSONres.items[i].value);
									if(eval(JSONres.items[i].selected)){
										selectedIndex=i;
									}
								}
								ddl.options.add(opt);
							}
							ddl.selectedIndex = selectedIndex;
							ddl.disabled = false;
							jObj.removeAttr('data-sv');
							if(hasFocus(jObj)){ddl.focus();}
						} else {
							ddl.disabled=true;
							ddl.options.add(new Option('Ingen data...', "-1"));
						}
						if(eval(postUpdateFn)==null){
							$(ddl).change();
						}else{
							postUpdateFn(ddl);
						}
					}
				}
			}
		}); 
	}
}

function GoogleGeocode(apiKey) {
	this.apiKey = apiKey;
	this.geocode = function(address, callbackFunction) {
		jQuery.ajax({
			dataType: 'jsonp',
			url: 'http://maps.google.com/maps/geo?output=json&oe=utf8&sensor=false&key=' + this.apiKey + '&q=' + address,
			cache: false,
			async: true,
			success: function(data){
				var result = {};
				var bHasValue = false;
				if(data.Status.code==200) {
					if(data.Placemark && data.Placemark.length>0){
						var place = data.Placemark[0];
						if(place.AddressDetails && place.AddressDetails.Country && place.AddressDetails.Country.AdministrativeArea){
							ad = place.AddressDetails.Country.AdministrativeArea;
							if(ad.Locality && ad.Locality.PostalCode && ad.Locality.PostalCode.PostalCodeNumber){
								result.zip = ad.Locality.PostalCode.PostalCodeNumber;
								bHasValue = true;
							}else if(ad.SubAdministrativeArea && ad.SubAdministrativeArea.Locality && ad.SubAdministrativeArea.Locality.PostalCode && ad.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber){
								result.zip = ad.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
								bHasValue = true;
							}
						}
						if(place.Point && place.Point.coordinates && place.Point.coordinates.length >= 2){
							result.longitude = place.Point.coordinates[0];
							result.latitude = place.Point.coordinates[1];
							bHasValue = true;
						}
					}
				}
				if(bHasValue){
					eval(callbackFunction(result));
				} else{
					eval(callbackFunction(null));
				}
			}
		  });
	};
}

var googleMapsApiKey = '';
var geoCoder = null;
var mapIcons = {GarageSmall:'/css/img/map_markers/garage_small.png',GarageLarge:'/css/img/map_markers/garage.png'};

if(window.location.hostname.indexOf('.cms.seek4cars.local',0) > 0){
	googleMapsApiKey = 'ABQIAAAAtfaVcwi1OKzBnhS-kNbMNxSSqGYLURAdm8r590F28dCB1fQ3SxQ1sR31prB08U3VEO5NjYqp68oq6w';//	*.cms.seek4cars.local
}else if(window.location.hostname.indexOf('.cms.seek4cars.net',0) > 0){
	googleMapsApiKey = 'ABQIAAAAtfaVcwi1OKzBnhS-kNbMNxSynuBX8aSShz_O5MbSB7k-EkYLJhQ99Yk52fldHRE4KPXeiGuYbQAoeQ';//	*.cms.seek4cars.net
}
