/**
* @Object: Props
* @Author: Fabian Moya - Avventa World Wide
* @Description: All Manual Properties hosted in a JSON Object
*/
$(document).ready(function() {
//QA

var Url= location.href; 
var suffix=getSuffixDomain(Url);
var lang =getLanguage(suffix);
var country =getCountry(suffix);

var country_word= '';
if (country=='us')
    country_word= 'north america';
else
country_word= 'canada';
//country_word= 'north america'
var temp_lang =lang.split('-');
var simple_lang = temp_lang[1];

var pageName=GetPageName(Url);
var sectionName=GetSectionName(Url);

var sectionAndpageName="";
//if (sectionName=="") sectionName="index";

if (sectionName==false)
{ 
if(pageName!="" && pageName !="index"){
sectionName=":";
sectionAndpageName=sectionName + pageName ;
s.channel = 'ci:'+ lang +':'+ pageName;
s.prop3='ci:'+ lang +':'+ pageName;
s.pageName ='ci:'+ lang + sectionAndpageName;
}else//we are on the index
{

sectionAndpageName="home" ;
s.channel = 'ci:'+ lang +':'+ "home";
s.prop3='ci:'+ lang +':'+ "home";
s.pageName ='ci:'+ lang +':'+ "home";
pageName="index";
}
}else{
if (pageName!="")
sectionAndpageName=":" + sectionName + ":" +pageName ;
else sectionAndpageName=":" + sectionName ;
s.channel = 'ci:'+ lang +':'+ $.baseSection(sectionName);
s.prop3='ci:'+ lang +':'+ $.baseSection(sectionName);
s.pageName ='ci:'+ lang + sectionAndpageName;
}

s.server ="";
s.pageType="";
s.prop1 = "ci";
s.prop2="ci:"+lang+"";
s.prop4="";
s.prop12=country;
s.prop13=country_word;
s.prop14=simple_lang;
s.eVar8=country;
s.eVar9=country_word;
s.eVar10=simple_lang;


for( propIndex in props ){
 //this will return index
 var pgName=propIndex;
  for( propId in props[ propIndex ] ){
  
   //propID is the variable name

  //console.debug(props[ propIndex ][ propId ]); // value of the variabels
  var type= propId;
  //console.debug(s[ type + propId ]);
 
  if (pgName==pageName)
  {

 s[ propId ] = (props[ propIndex ][ propId ]);// this will set the manual variables dynamically
  }
  }

}
/* Link all print buttons*/
$('a.print').click(function(){  var s= s_gi(s_account);
                                    s.linkTrackVars='prop32,eVar28,events';
                                    s.linkTrackEvents='event13';
                                    s.prop32=s.pageName;
                                    s.eVar28=s.pageName;
                                    s.events='event13';
                                    s.tl(this,'o','ci_'+ lang +'_print');
});
/* Email Link */
$('#emailPageLink').click(function(){  var s= s_gi(s_account);
                                    s.linkTrackVars='prop32,eVar28,events';
                                    s.linkTrackEvents='event18';
                                    s.prop32=s.pageName;
                                    s.eVar28=s.pageName;
                                    s.events='event18';
                                    s.tl(this,'o','ci_'+ lang +'_email');
});
/* Country language links */
$('.countryLink a').click(function(){  
 var countrycode = '';
 var country = jQuery.trim($(this).attr('href'));
 switch(country){
 case "http://www.combined.com.au": countrycode = 'au'; break;
 case "http://www.combined.at": countrycode = 'at'; break;
 case "http://www.combined.ca/home.html": countrycode = 'ca-en'; break;
 case "http://www.combined.ca/fr/": countrycode = 'ca-fr'; break;
 case "http://www.combined.de": countrycode = 'de'; break;
 case "http://www.combinedinsurance.ie": countrycode = 'ie'; break;
 case "http://www.combinedinsurance.co.nz": countrycode = 'nz'; break;
 case "http://www.combinedinsurance.com.pt/": countrycode = 'pt'; break;
 case "http://www.combined.es": countrycode = 'es'; break;
 case "http://www.combined.co.th": countrycode = 'th'; break;
 case "http://www.combinedinsurance.co.uk": countrycode = 'uk'; break;    case "http://www.combinedinsurance.com": countrycode = 'us-en'; break;   }
 
var s= s_gi(s_account); 
s.linkTrackVars='eVar6,events';
s.linkTrackEvents='event6';
s.eVar6=countrycode;
s.events='event6';
s.tl(this,'o','ci_'+lang+'_link_to_'+ countrycode );
});

/* My Reports */
$("a[href*='myreports.combined.com']").click(function(){
   var s= s_gi(s_account); 
s.linkTrackVars='prop33,eVar29,events';
s.linkTrackEvents='event9';
s.prop33='My Reports:'+lang;
s.eVar29='My Reports:'+lang;
s.events='event9';
s.tl(this,'o','ci_'+lang+'_access_myreports}');
});
/* Lead Ordering and Delivery System */
$("a[href*='www.myleadcards.com']").click(function(){
   var s= s_gi(s_account); 
s.linkTrackVars='prop33,eVar29,events';
s.linkTrackEvents='event9';
s.prop33='Lead Ordering and Delivery System:'+lang;
s.eVar29='Lead Ordering and Delivery System:'+lang;
s.events='event9';
s.tl(this,'o','ci_'+lang+'_access_leadordering}');
});

/*Download Pdf*/ 

     $("a[href*='http://1505.ic-live.com/cr.php']").click(function(){
                  
                   var s= s_gi(s_account);
                                          s.linkTrackVars='prop7,eVar7,events';
                                          s.linkTrackEvents='event4';
                                          var url2=$(this).attr('href');
                                          var ud1= s.getQueryParam('ud1','',url2);
                                          var ic_url=s.getQueryParam('ic_url','',url2);
                                          s.prop7=ud1 +':'+ ic_url;
                                          s.eVar7=ud1 +':'+ ic_url;
                                          s.events='event4';
                                          s.tl(this,'d','ci_'+lang+'_download}');
      }); 
      
      
    /*Download PDF*/
    $("a[href*='.pdf']").click(function(){
        var s= s_gi(s_account);        
        //s.tl(this,'d','ci_' + lang + '_download');
        s.tl(this, 'd');
    });      


/* Stories page */
var story = $('.storyName').length > 0;
if (story){ 
var intNameHeight = 0;
var intBorderHeight = 0;
for(var heightloop=0; heightloop < ($("#mainContent").find('dt').length);heightloop++){
tempBorder = $("#mainContent").find('dt').eq(heightloop).css("borderTopWidth").split("px");
intBorderHeight = intBorderHeight + parseInt(tempBorder[0]);

intNameHeight = intNameHeight + $("#mainContent").find('dt').eq(heightloop).outerHeight(); 
} //}
intNameHeight = intNameHeight - 11; 
$('#mainContent').find('span.storyName').wrapInner("<a href=\"javascript:;\"></a>");
$('#mainContent').find('dt:first').addClass("first");
$('#mainContent').find('dd').hide().end().find('dt').click(function() { 

$('#mainContent').find('dd').hide(); 
$('#mainContent').find('dt').removeClass("on");
$(this).next().toggle(); 
$(this).addClass("on"); 
if($(this).next().height() < intNameHeight){
$('#mainContent').find('dl').css("height",intNameHeight); 
$(this).next().css("height",intNameHeight);
}else{
$('#mainContent').find('dl').css("height",$(this).next().height()); 
} 
// Fire omniture tags
var s= s_gi(s_account);
s.linkTrackVars='prop32,eVar28';
s.prop32=jQuery.trim($(this).find('.storyName a').text());
s.eVar28=s.prop32;

s.tl(this,'o','ci_'+ country +'_'+lang+ '_' + pageName.replace(/-/g,'_'));
}); 
var query = $.query(location.search);
var bolfoundstory = false;
for (var k in query){
if (k === 'storyid'){
var storyIndex = -1;
if ($.isInt(query[k])){
var storyid = 'storyid-'+query[k];
$('#mainContent').find('dt').each(function (obj){
if ($('#mainContent').find('dt').eq(obj).attr('id') == storyid) 
storyIndex = obj;
});
}
else
{
if (!$.isInt(query[k])){
$('#mainContent').find('dt a').each(function (obj){
if ($('#mainContent').find('dt a').eq(obj).text() == query[k]) 
storyIndex = obj;
});
}
}
if (storyIndex >= 0){
$('#mainContent').find('dt').eq(storyIndex).addClass("on");
$('#mainContent').find('dd').eq(storyIndex).show();
s.prop32=jQuery.trim($('#mainContent').find('dt a').eq(storyIndex).text());
if($('#mainContent').find('dd').eq(storyIndex).height() < intNameHeight){
$('#mainContent').find('dl:first').css("height",intNameHeight);
$('#mainContent').find('dd').eq(storyIndex).css("height",intNameHeight);
}else{
$('#mainContent').find('dl:first').css("height",$('#mainContent').find('dd').eq(storyIndex).height());
} 
bolfoundstory = true;
}
} 
}; 
if (!bolfoundstory){
$('#mainContent').find('dt:first').addClass("on"); 
$('#mainContent').find('dd:first').show();
s.prop32=jQuery.trim($('#mainContent').find('dt:first a').text());
if($('#mainContent').find('dd:first').height() < intNameHeight){
$('#mainContent').find('dl:first').css("height",intNameHeight);
$('#mainContent').find('dd:first').css("height",intNameHeight);
}else{
$('#mainContent').find('dl:first').css("height",$('#mainContent').find('dd:first').height());
} 
}; 

s.eVar28=s.prop32;
}


/* Search Results */
var searchTerm = s.getQueryParam('q');
if (searchTerm){
s.eVar5 = searchTerm.replace(/\+/g,' ');
s.events='event5';
s.prop5 = s.eVar5;
}
/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
var s_code = s.t(); 
if(s_code){document.write(s_code) };
});

function getDomain(Url){
Url = Url.replace("http://", " ");
var separateUrl = Url.split('/'); 
var fileName = separateUrl[separateUrl.length - 1];
var DomainName =separateUrl[0];//get domain
//console.debug(DomainName);

return DomainName;
}
function getSuffixDomain(Url){

var DomainName = getDomain(Url);
var splitDomain =DomainName.split('.');
var suffix = splitDomain[splitDomain.length-1];

if (suffix=="ic-creative"){//if QA environment

if (splitDomain[1]=="ca")// if is QA for CA environment
{

 suffix="ca";
if (DomainName.indexOf('/fr/')>= 0)//FR site
{
suffix="ca/fr";

}else
 {
 suffix="ca";//CANADA EN
 
 }
 
}

 }
 else
 
 {
if (suffix.indexOf('com') >= 0)// US Site
 {
//console.debug("com");
 suffix="com";

 }
 else if (suffix.indexOf('ca') >= 0)// canada domain
 {
 if (Url.indexOf('/fr/')>= 0)//FR Site
 {
 // console.debug("ca/fr");
  suffix="ca/fr";
 }else
 {
//console.debug("ca");// CA Site
  suffix="ca";
  
 }
 }
 }
//console.debug(suffix);
return(suffix);
}
function getLanguage(suffix){
var Lang="";
if (suffix == 'com')// US Site
 {
// console.debug("com");
 Lang="us-en";
 }  else
 if (suffix =='ca/fr')//FR Site
 {
  //console.debug("ca/fr");
  Lang="ca-fr";
 }else
 {
 // console.debug("ca");// CA Site
  Lang="ca-en";
  
 }
 

return Lang;
}

function getCountry(suffix)//get country
{
var Country="";
if (suffix == 'com')// US Site
 {
// console.debug("com");
 Country="us";
 }  else
 if (suffix =='ca/fr')//FR Site
 {
//  console.debug("ca/fr");
  Country="ca";
 }else
 {
//console.debug("ca");// CA Site
  Country="ca";
 
  
 }
 

return Country;
}

function GetPageName (Url)
{
Url = Url.replace("http://", " ");
var separateUrl = Url.split('/'); 
var fileName = separateUrl[separateUrl.length - 1];
var pageName= fileName.split('.');

return pageName[0];
}


function GetSectionName (Url)
{
Url = Url.replace("http://", " ");
var separateUrl = Url.split('/');
var fileName = separateUrl[separateUrl.length - 2];
//var sectionName= fileName.split('.');
var sectionName='';
    if (separateUrl.length > 1) sectionName = separateUrl[1];
var no_section=false; 
$section="";
 if(sectionName.indexOf('.html') >= 0) {  
  return false;
 }
 else {
 sectionName='';
 for(i=1; i<=separateUrl.length-2;i++) {
 if (separateUrl[i] !='')
 {
if(i==1) sectionName += separateUrl[i];
else sectionName += ':'+separateUrl[i]; 
 }
 }
 }
 
//  console.log("Section Name: " + sectionName);  
 
return sectionName;
}

/**
*Get Base Section by Url
*/
jQuery.baseSection =  function(section) {
   var sectionSplited = section.split(':');
   if(sectionSplited[0]) {
    return sectionSplited[0];
   }
   
   return false;
}
jQuery.query = function(s) {
var r = {};
if (s) {
var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
q = q.replace(/\&$/, ''); // remove the trailing &
q = q.replace(/\?/,''); 
q = q.replace(/\</,''); 
q = q.replace(/\>/,''); 
jQuery.each(q.split('&'), function() {
var splitted = this.split('=');
var key = splitted[0];
var val = splitted[1];
// convert numbers
if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
// convert booleans
if (val == 'true') val = true;
if (val == 'false') val = false;
// ignore empty values
if (typeof val == 'number' || typeof val == 'boolean' || val.length > 0) r[key] = val;
});
}
return r;
}; 
jQuery.isInt = function (x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
 }
