// JavaScript Document

function zebra_table(){	
  $(".zebra tr:nth-child(even)").addClass("roweven");
}

function collapse(){	
	$(".section").hide(); //--> cannot change to display in print style    
	$(":header a").click(function() {                      
		$($(this).attr("href")).slideToggle();                      
		return false;
	});
}
//pdf and transaction pages open in new window

function externalLinks() {
	var h = window.location.host.toLowerCase();
	$("a[@href^='http']:not([@href^='http://" + h + "']):not([@href^='http://www." + h + "']), a[@href$='.pdf']").attr("target", "_blank");
}

function boxlarge_click(){	
	$("dl.calltoaction, dl.panel").click(function () {
		var url = $(this).find('a:first').attr('href');				
		if(url.indexOf('NASApp')>-1 || url.indexOf("https://www.apia.com.au/apia/")>-1){	
		  var firstLink = $(this).find('a:first');		  
		   quoteurl = getQuoteLinks(firstLink);
	       popupWindow(quoteurl, true);
		}else if($(this).find('a:first').attr('target')== "_blank"){	
		   popupWindow(url, false);		  
		}else{
		  location.href=url;
		}	
		return false;
    });	
}

function getQuoteLinks(element){	
  switch (true){			
	case element.hasClass("carinsurance"):
		quotelink = car_insurance_url;
		break;
	case element.hasClass("motorhomeinsurance"):
		quotelink = motorhome_insurance_url;
		break;			 
	case element.hasClass("boatinsurance"):
		quotelink = boat_insurance_url;
		break;	
	case element.hasClass("caravaninsurance"):
		quotelink = caravan_insurance_url;
		break;
	case element.hasClass("landlordinsurance"):
		quotelink = landlord_insurance_url;
		break;	
	case element.hasClass("myinsurance"):
		quotelink = my_insurance_url;
		break;	
	}	
	return quotelink
}

function popupWindow(url, secure){
	 features=secure?"resizable=1,scrollbars=1,location=1,status=1":"resizable=1,scrollbars=1,location=1,status=1,toolbar=1,menubar=1"; 	 
	 w=window.open(url, "newwin", features);  
	 w.focus();	 	
}

function setQuoteLinks(){
  if ($.cookie('accept_terms')) {	
    $("#getaquoteorretrieveasavedquote a, #getaquote").attr("href","/apia/get-insurance-quote"); 
    $("a#homeselector").attr("href","/apia/home-insurance-selector");
	$("a.myinsurance, a.myinsurancelogin, a.myinsuranceregister").click(function() { 
		popupWindow(my_insurance_url,true);	
		return false;	
	});
	$("a.renewyourpolicy").click(function(){ 
		popupWindow(renew_policy_url,true);	
		return false;									  
	});
  }  	
}

$(document).ready(function(){ 
	zebra_table();
	collapse();
	externalLinks();
	boxlarge_click();
	setQuoteLinks();
});