$.extend({
  getUrlVars: function(url){
    var vars = [], hash;
    if (url == undefined){
    	url = window.location.href;
    }
    var hashes = url.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
  		//alert(hash[0]+"="+hash[1]);
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

function equalHeight(group) {
	tallest = 0;
	extended = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}



function isValidEmailAddress(emailAddress) {
	emailAddress = emailAddress.replace("+","stortfedtvinvinvinplus");
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function checkemail(email,callback){
	if (isValidEmailAddress(email)){
		$.get('/admin/validate_email.asp',{email:email},callback);
	}
}

function listsForEmail(email,callback){
	var url = '/_json/mailchimp.asp?email='+email+'&action=listsForEmail';
	$.ajax({
	  url: url,
	  dataType: 'json',
	  success: callback
	});
}

function openNewsletter(){
	$.fancybox({'type':'iframe', 'width':500, 'height':300, 'href':'/test/newsletter_popup.asp'});
}

function login(){
	$.fancybox({'type':'iframe', 'width':500, 'height':500, 'href':'/mit/login_popup.asp'});
}

function openContact(){
	$.fancybox({'type':'iframe', 'width':500, 'height':300, 'href':'/vinvinvin/kontakt/kontakt_popup.asp','margin':0,'padding':0});
}
function openLocation(address){
	$.fancybox({'type':'iframe', 'width':700, 'height':500, 'href':'/mit/location_popup.asp?address='+address,'margin':0,'padding':0});
	
}
function yearModal(){
	$.fancybox({'type':'iframe', 'width':400, 'height':140, 'href':'/vin/yearmodal.asp'});
}
function nl2(str,breakTag) {
	return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
function striphtml(str){
	return (str + '').replace(/(<([^>]+)>)/ig,"");
}
function replaceapos(str){
	return str.replace(/'/g, ' ');
	
}
var daad;

$(document).ready(function(){
	var footerAd = $("#footerAd");
	
	var topAd = $("#topAd");
	var adHTML = '';
	adHTML += '<a id="anchor" href="" target="" style="text-decoration: none !important;">';
	adHTML += '<div id="ad_bar">';
	adHTML += '<div id="headline"></div>';
	adHTML += '<div id="msg"></div>';
	adHTML += '<div id="url"></div>';
	adHTML += '</div>';
	adHTML += '</a>';
	
	footerAd.html($(adHTML));
	topAd.html($(adHTML));
	
	$.ajax({url:"/_json/ad.asp?1="+new Date()
		,data:{daad:daad}
		,contentType: "application/json"
		,dataType: 'json'
		,error:function(XMLHttpRequest, textStatus, errorThrown){
			alert("XMLHttpRequest=\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown+"\nError: "+XMLHttpRequest.responseText);}
		,success:function(data){
			if (data != undefined){
			topAd.find("#headline").text(data.headline);
			footerAd.find("#headline").text(data.headline);
			topAd.find("#msg").html(striphtml(data.msg));
			footerAd.find("#msg").html(striphtml(data.msg));
			topAd.find("#url").text(data.display_url);
			footerAd.find("#url").text(data.display_url);
			topAd.find("#anchor").attr("href",data.url);
			footerAd.find("#anchor").attr("href",data.url);
			topAd.find("#anchor").attr("target", data.target);
			footerAd.find("#anchor").attr("target", data.target);
			}
			setTimeout(function(){
				$(".adtopcontainer").animate({top:0});
			},50);
		}
	});
	
	
	//find all form with class jqtransform and apply the plugin
	
    //$("form.jqtransform").each(function(){$(this).jqTransform();});
    
	
	/*$(document).bind('focus',function(){
		$("#shops").slideUp('fast');
		$("#aboutWebsite").slideUp('fast');
	});*/
	if ($('#aboutDealerButton').size() > 0)
		$('#aboutDealerButton').bubbletip($('#aboutDealer'), {deltaDirection: 'down', deltaPosition: 50, delayHide:100, offsetTop: 2, offsetLeft: 160, animationEasing:"linear", animationDuration:0});
	if ($('#aboutDrinkerButton').size() > 0)
		$('#aboutDrinkerButton').bubbletip($('#aboutDrinker'), {deltaDirection: 'down', deltaPosition: 50, delayHide:100, offsetTop: 2, animationEasing:"linear", animationDuration:0});
	if ($('#shareButton').size() > 0)
		$('#shareButton').bubbletip($('#shareWindow'), {deltaDirection: 'down', deltaPosition: 50, delayHide:100, offsetTop: 2, animationEasing:"linear", animationDuration:0});
	if ($('#aboutWebsiteButton').size() > 0)
		$('#aboutWebsiteButton').bubbletip($('#aboutWebsite'), {deltaDirection: 'down', deltaPosition: 50, delayHide:100, offsetTop: 2, offsetLeft: -180, animationEasing:"linear", animationDuration:0});
});
