/*
* CHANGED/Hoek/27-10-2011/AEGNLANLY-95 - Changed typo in downloads.
* CHANGED/Hoek/13-10-2011/AEGNLANLY-95 - Removed all but one (document).ready.
* CHANGED/Hoek/10-10-2011/AEGNLANLY-95 - Change for tabs, PDF-downloads.
* CHANGED/Hoek/01-09-2011/AEGNLANLY-95 - Changes after test Adversitement; cleaned all code.
*/

$(document).ready(function() {
// event for Sitecatalyst tracking.
$("div.functions a").click(function(a) {
	AB_WA.ttd("top-page-buttons", {"title": $(this).attr("title").toLowerCase()});
});
// event for downloads
$("ul.downloads li a").click(function(a) {
	AB_WA.ttd("download", {"title": $(this).html().toLowerCase()});
});
// track klanten service
var tmp=location.pathname;
if (tmp.substring(0, 27) == "/particulier/klantenservice") {
	$("div.content-part a:has(img) img").click(function(a) {
		var title=$(this).attr("src").toLowerCase();tmp=getPageName(tmp);
		//var linkName=tmp.substr(tmp.lastIndexOf(':')+1,tmp.length);
		var str=$('div.left-column h1').html();
		var subject ='';
		if(str != null) { subject=str; } 
		if(title.substring(title.length-6, title.length+1) == 'Ja.jpg' || title.substring(title.length-6, title.length+1) == 'ja.jpg' ) {
			$(window).unload( function() { AB_WA.ttd("feedback-yes", {title: subject}); });
		} 
		else if(title.substring(title.length-7, title.length+1) == 'Nee.jpg' || title.substring(title.length-7, title.length+1) == 'nee.jpg') {
			$(window).unload( function() { AB_WA.ttd("feedback-no", {title: subject}); });
		}
	});
}
// track the use of the four different links to 'Mijn AEGON'
var tmp1=location.pathname;
if (tmp1.substring(0,37) == "/particulier/klantenservice/mijnaegon") {
	$("div.highlight-white p a").click(function() {
		if ($(this).html() == 'Toegang aanvragen') {
				var mijnaegon_name=$(this).parents("div").find("h2 a").html().toLowerCase();
				AB_WA.ttd("requestpermission", {"title": mijnaegon_name});
		}
	});
}
//measuring PDF-downloads
$("div a[href$=.pdf]").click(function() {
	var pdfName=$(this).html().toLowerCase();
	AB_WA.ttd("download", {"title": pdfName});
});
// event for all banners on a page
//$("div.related-content a:has(img)").click(function(a) {
$("a.inlinebanner:has(img)").click(function(a) {
	var bannertitle=$(this).attr("title").toLowerCase();
	AB_WA.ttd("internal-banner", {"title": bannertitle});
});
// event for directnaar links
var tmp=getPageName(location.pathname);
$("div.directnaar-links a").click(function(a) {
	AB_WA.ttd("direct-to", {"title": (tmp + " - " + $(this).html().toLowerCase())});
});
// event for pagenumber in searchresults
$("div.paging-center a").click(function(a) {
	AB_WA.ttd("searchresults", {"title": $(this).html().toLowerCase()});
});
// event for login links in servicemenu
$("div.login-bottom div.bottom-line a").click(function(a) {
	var s=s_gi(s_account);
	s.linkTrackVars='eVar20,events';
	s.linkTrackEvents='event7';
	s.eVar20='Inloggen - ' + $(this).attr("title");
	s.events='event7';
	s.trackExternalLinks=false;
	s.tl(this,'e','Inloggen - ' + $(this).attr("title"));
});
// event for tabs
$("div.box-tabs ul.tabs li a").click( function() {
	AB_WA.ttd("formtab", {"title": jQuery.trim($(this).html().toLowerCase())});
});
// EXIT LINKS
// event for waardevolle exit links (using param 'wel')
$("a[href*=wel\=]").click(function(a) {
	var myLinkName=gup($(this).attr("href"), 'wel');
	if (myLinkName == '') {myLinkName='undefined';} 
	myLinkName=myLinkName.toLowerCase();
	AB_WA.ttd("exitlink", {"title": myLinkName});
});
// event for waardevolle exit links (simplename contains 'wel_')
$("a[href*=wel_]").click(function(a) {
	var tmp=$(this).attr("href");
	var myLinkName=tmp.substr(tmp.indexOf('wel_')+4,tmp.length);
	if (myLinkName == '') {myLinkName='undefined';}
	myLinkName=myLinkName.toLowerCase();
	AB_WA.ttd("exitlink", {"title": myLinkName});
});
// event for exit link to AEGON Bank auto calculator (from qqauto).
$("#qqauto").click(function(a) {
	var tmp=$(this).attr("href");
	var myLinkName='abb_auto_online_afsluiten_qq';
	AB_WA.ttd("exitlink", {"title": myLinkName});
});
// event for clicks on form tabs
$("div#tabs li a.isformtab").click(function(a) {
	AB_WA.tabblad("formtab", {"title": jQuery.trim($(this).html().toLowerCase())});
});
// event for clicks on other tabs
$("div#tabs li a.isothertab").click(function(a) {
	AB_WA.tabblad("othertab", {"title": jQuery.trim($(this).html().toLowerCase())});
});
// end of (document).ready
});
// HELPER METHODS. DO NOTE REMOVE OR CHANGE!
// convert path to pageName
function getPageName(url) {
    var pageName="";
    url_elems=url.split("/");
    for (var i=0;i<url_elems.length;i++) {
        var cur_elem=url_elems[i];
        if (cur_elem != '') {
            pageName=pageName + cur_elem;
            if (i <= url_elems.length) {
                pageName=pageName + ":";
            }
        }
    }
    // remove last colon
    pageName=pageName.substring(0,pageName.length-1);
    // add homepage label for kanaalhomepages
    if (pageName == 'particulier' || pageName == 'zakelijk' || pageName == 'overaegon') {
        pageName=pageName + ":homepage";
    }
    // if we're on the homepage the pageName is empty
    if (pageName == '') {
        pageName="homepage";
    }
    // replace view with colon
    var view=gup(window.location.href,'view');
    if (view != '') {
        pageName=pageName + ':' + view.replace('+',' ');
    }
    // convert to lowercase
    pageName=pageName.toLowerCase();
    // finally we can return the pageName
    return pageName;
}

// get proper name of the Channel
function getChannelName(rawName) {
    var channelName="home";
    if (rawName != null && rawName != '') {
    	channelName=rawName.toLowerCase();
    }
    return channelName;
}
// get value op productgroep in bestellen en downloaden form
function getProductGroup() {
    var pg=$('option:selected', 'div.download-content div.search-box dl select#productgroep').text();
    if (pg != null && pg != undefined && pg != "") {
        return pg;
    } else {
        return "";
    }
}
function getProductType() {
    var pt=$('option:selected', 'div.download-content div.search-box dl select#producttype').text();
    if (pt != null && pt != undefined && pt != "") {
        return pt;
    } else {
        return "";
    }
}
// the ul element only exists if a user is logged in
function zakelijkLoggedIn() {
    var isLoggedIn=false;
    var hasReports=$('div.left-column ul').attr('class');
    if (hasReports != null) {
        isLoggedIn=true;
    }
    return isLoggedIn;
}
// get url parameter
function gup(theUrl,name) {
    name=name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS="[\\?&]"+name+"=([^&#]*)";
    var regex=new RegExp( regexS );
    var results=regex.exec( theUrl );
    if(results == null)
        return "";
    else
        return results[1];
}
// get info on all products in a cart
function getProductString() {
  var allProducts="";
  $("div.snelmenu-menu table tbody td").each( function () {
          // first get the title
          if ($(this).attr('class') == 'first-col'){
              var tmp=$(this).children("a");
              if (tmp.html() != null) {
                  allProducts += ';'+ tmp.html();
                  //alert(tmp.html() + " - " + allProducts);
              }
          }
          // then get the ammount
          if ($(this).attr('class') == 'input-col'){
              var tmp=$(this).children("input");
              if (tmp.attr('value') != null) {
                  allProducts += ';'+ tmp.attr('value');
                  //alert(tmp.attr('value'));
              }
          }
          // then get the subtotal
          if ($(this).attr('class') == 'subtot-col'){
              var tmp=$(this).html();
              if (tmp != null) {
                  if (tmp.length > 2) {
                      tmp=tmp.substr(2,tmp.length);
                      tmp=tmp.replace(',','.');
                  }
                  allProducts += ';'+ tmp + ",";
                  //alert(allProducts);
              }
          }
  });
  //alert("allProducts: " + allProducts);
  // return result
  return allProducts;
}
// actionCodes can be used for sfeerbeeld flash
function getActionCode() {
  var actionCode=$('div #actionCode').attr('title');
  if (actionCode == undefined) {
      actionCode="";
  }
  //alert(actionCode);
  return actionCode;
}
// eof
