Razsor.WebTracker = Razsor.WebTracker || {};

$(document).ready(function() {
                    
					$('.insurance li a.insurance-link').after('<span class="tooltip-icon">info</span>');
					
					$(".tooltip-icon").click(function(event){
						$(".tooltip").remove();
						$("body").prepend('<div class="tooltip"><div class="tooltip-header">Click here if you would like to...<a href="#">Close</a></div><div class="tooltip-content"><ul><li><a href=""><span>Review, change or buy your quote</span></a></li></ul></div><span class="tooltip-logo"></span><span class="tooltip-arrow"></span></div>');
						$(".tooltip").hide();
						$(".tooltip .tooltip-content ul li a").attr("href",$(this).parent().find("a").attr("href"));
						var leftPos = parseInt(event.pageX, 10) + 10;
						var topPos = parseInt(event.pageY, 10) - 75;
						$(".tooltip").css({'left' : leftPos + 'px', 'top' : topPos + 'px'}).fadeIn();			
					});
					
					$(".tooltip .tooltip-content a").live('click', function(event){
					    razsor.commonfunctions.CommonFunctions.newWindow(event, this);
					});
					
					$(".tooltip .tooltip-header a").live('click', function(event){
						event.preventDefault();
						$(".tooltip").fadeOut(function(){
							$(".tooltip").remove();
						});	
					});
					
					var tooltipTimeout;
					
					$(".tooltip").live('mouseenter', function(){
						clearTimeout(tooltipTimeout);
					});
					
					$(".tooltip").live('mouseleave', function(){
						tooltipTimeout = setTimeout(function()
				         { 
				            $(".tooltip").fadeOut(function(){
								$(".tooltip").remove();
							});
				         }, 2000);
					});

                
	                // Contextual stock strip & stocklist
	                $(".rzc-c-s-strip .hlisting, .rzc-stocklist .hlisting")
	                        .each(
	                                function() {
		                                $(this)
		                                        .click(
		                                                function(event) {
			                                                var jTarget = $(event.target);
			                                                if (jTarget
			                                                        .hasClass('video-link')
			                                                        || jTarget.hasClass('stockFacebook')
			                                                        || jTarget.hasClass('stockTwitter')
			                                                        || jTarget.hasClass('tooltip-icon')
			                                                        || jTarget.hasClass('finance-info-button')
			                                                        || jTarget.hasClass('finance-info-button-outer')
			                                                        || jTarget.hasClass('advert-services-title')
			                                                        || jTarget.hasClass('permalink')
			                                                        || jTarget.hasClass('advert-title-inner')) {
				                                                return;
			                                                }
			                                                document.location = $(this).find("a").attr("href");
		                                                })
		                                        .hover(
		                                                function() {
			                                                $(this).addClass("hover");
		                                                },
		                                                function() {
			                                                $(this).removeClass("hover");
		                                                });
	                                });
                });

//add all future functions to this object
//move existing ones out of global scope and into here
razsor.definePackage("razsor.commonfunctions");
razsor.defineClass({
    "_className" : "CommonFunctions",
    "_package" : razsor.commonfunctions,
    "_static" : {
        "newWindow" : function(event,eventTarget){
            event.preventDefault();
            var newWindow = window.open($(eventTarget).attr("href"),'_blank');
            newWindow.focus();
        },
        "setClassToColorbox" : function(className) {
            $('#colorbox').removeClass();
            $('#colorbox').addClass(className);
        }
    },
    "_class" : {
        "init" : function() {
            
        }
    } 
});

function equalHeight(group) {
	var tallest = 0;
	if (group.size() > 1) {
		group.each(function() {
			var thisHeight = $(this).height();
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
}

function balanceHeightOfPromotions() {
	var $promotions = $('.rzc-promotions');
	var $h3span = $('h3 span', $promotions);
	var $balance = $('.balance', $promotions);
	var $terms = $('.terms', $promotions);
	var $offerEnds = $('.offer-ends', $promotions);

	$h3span.css('height', 'auto');
	$balance.css('height', 'auto');
	$terms.css('height', 'auto');
	$offerEnds.css('height', 'auto');

	equalHeight($h3span);
	equalHeight($balance);
	equalHeight($terms);
	equalHeight($offerEnds);
}

function balanceHeightOfPromotionsAfterTimeout(timeout) {
	setTimeout(function() {
		balanceHeightOfPromotions();
	}, timeout);
}

function showAdvertVideoPopup() {
	$('.rzc-advert-video a').colorbox({
	    iframe : true,
	    innerWidth : 425,
	    innerHeight : 344,
	    onLoad : function() {
	        razsor.commonfunctions.CommonFunctions.setClassToColorbox('video-popup');
	    }
	});
}

function showStocklistFinanceVideoPopup(){
	$('.rzc-stocklist-finance-video a').colorbox({
	    iframe : true,
	    innerWidth : 425,
	    innerHeight : 344,
	    onLoad : function() {
	        razsor.commonfunctions.CommonFunctions.setClassToColorbox('video-popup');
	    }
	});
}

Razsor.WebTracker.Utils = {

    recordSendEmailVisit : function() {
	    if (!sendEmailFormTrackerKey) {
		    sendEmailFormTrackerKey = "";
	    }
	    Razsor.WebTracker.Utils.recordVisit(sendEmailFormTrackerKey);
    },

    recordSendFinanceVisit : function() {
	    if (!sendFinanceEmailFormKey) {
		    sendFinanceEmailFormKey = "";
	    }
	    Razsor.WebTracker.Utils.recordVisit(sendFinanceEmailFormKey);
    },

    recordSendTestDriveVisit : function() {
	    if (!sendTestDriveEmailFormKey) {
		    sendTestDriveEmailFormKey = "";
	    }
	    Razsor.WebTracker.Utils.recordVisit(sendTestDriveEmailFormKey);
    },

    recordStockListVisit : function(stockListKey) {
	    Razsor.WebTracker.Utils.recordVisit(stockListKey);
    },

    recordSendPartExchangeVisit : function() {
	    if (!sendPartExchangeEmailFormKey) {
		    sendPartExchangeEmailFormKey = "";
	    }
	    Razsor.WebTracker.Utils.recordVisit(sendPartExchangeEmailFormKey);
    },

    recordSendMotServiceVisit : function() {
	    if (!sendMotServiceEmailFormKey) {
		    sendMotServiceEmailFormKey = "";
	    }
	    Razsor.WebTracker.Utils.recordVisit(sendMotServiceEmailFormKey);
    },
    
    recordFinanceApplyNowVisit : function() {
	    if (typeof(sendFinanceApplyNowFormKey) !== 'undefined') {
              Razsor.WebTracker.Utils.recordVisit(sendFinanceApplyNowFormKey);
	    }
    },

    bindEvisitToShowcaseVehicleVideo : function(url) {
	    $(".rzc-showcase-vehicle .rzc-advert-video a").click(function() {
		    Razsor.WebTracker.Utils.recordVisitForUrl(url);
	    });
    },

    
    bindEvisitToAdvertDetailPhotoLink : function(advertTitle, url,
            advertPhotoViewWebTrackerKey) {
	    $("#advertDetailComponent .images a").click(
	            function() {
		            Razsor.WebTracker.Utils.recordVisitForKeyData(advertTitle,
		                    url, advertPhotoViewWebTrackerKey);
	            });
    },

    bindEvisitToAdvertDetailEnlarge : function(advertTitle, url,
            advertPhotoViewWebTrackerKey) {
	    var advertDetailComponet = $("#advertDetailComponent");
	    advertDetailComponet
	            .find(
	                    ".image_v .item img, .image_v .item span, .image .item img, .image .item span")
	            .click(
	                    function() {
		                    Razsor.WebTracker.Utils.recordVisitForKeyData(
		                            advertTitle, url,
		                            advertPhotoViewWebTrackerKey);
	                    });
    },

    bindEvisitToShowcaseVehiclePhotosPopup : function(advertTitle, url,
            popUpWebTrackerKey, advertPhotoViewWebTrackerKey) {
	    $(".rzc-showcase-vehicle .images a").click(
	            function() {
		            Razsor.WebTracker.Utils.recordVisitForUrl(url);
		            Razsor.WebTracker.Utils.recordVisitForKeyData(advertTitle,
		                    url, popUpWebTrackerKey);
		            Razsor.WebTracker.Utils.recordVisitForKeyData(advertTitle,
		                    url, advertPhotoViewWebTrackerKey);
	            });
    },

    bindEvistToStocklistAdvertVideo : function(id, url) {
	    $("#" + id + " .rzc-advert-video a").click(function() {
		    Razsor.WebTracker.Utils.recordVisitForUrl(url);
	    });
    },

    bindEvisitToStocklistVideoForPopUps : function(id, advertTitle, url,
            webTrackerKey) {
	    $("#" + id + " .rzc-advert-video a").click(
	            function() {
		            Razsor.WebTracker.Utils.recordVisitForKeyData(advertTitle,
		                    url, webTrackerKey);
	            });
    },

    recordVisit : function(webTrackerKey) {
	    Razsor.WebTracker.Utils.recordVisitForKeyData(document.domain,
	            document.location, webTrackerKey);
    },

    recordVisitForKeyData : function(keyValue, url, webTrackerKey) {
	    var evastr0000 = "";
	    if (!ID_dcode_15243615) {
		    ID_dcode_15243615 = "";
	    }

	    evastr0000 = "&rq=" + escape(url);
	    evastr0000 += "&dc=" + escape(ID_dcode_15243615);

	    // get keydata values
	    evastr0000 += "&userid=" + escape(ID_uid_15243615);
	    evastr0000 += "&tt=k";
	    evastr0000 += "&ctid=" + escape(webTrackerKey);
	    evastr0000 += "&ctval=" + escape(keyValue);

	    var now = new Date();

	    var thetimeid = now.getTime();

	    evastr0000 += "&tc=" + thetimeid;

	    var imageTag = document.createElement("img");
	    imageTag.setAttribute("border", "0");
	    imageTag.setAttribute("alt", "eVisit");
	    imageTag.setAttribute("width", "1");
	    imageTag.setAttribute("height", "1");
	    imageTag.setAttribute("class", "tracking");
	    imageTag.setAttribute("src",
	            "http://extra.evisitanalyst.com/at/tagkd.php?" + evastr0000);

	    document.body.appendChild(imageTag);
    },

    recordVisitForUrl : function(url) {
	    var evastr0000 = "";
	    if (!ID_dcode_15243615) {
		    ID_dcode_15243615 = "";
	    }

	    evastr0000 = "&rq=" + escape(url);
	    evastr0000 += "&dc=" + escape(ID_dcode_15243615);
	    // get keydata values
	    evastr0000 += "&userid=" + escape(ID_uid_15243615);
	    evastr0000 += "&tt=t";
	    var now = new Date();
	    var thetimeid = now.getTime();
	    evastr0000 += "&tc=" + thetimeid;

	    var imageTag = document.createElement("img");
	    imageTag.setAttribute("border", "0");
	    imageTag.setAttribute("alt", "eVisit");
	    imageTag.setAttribute("width", "1");
	    imageTag.setAttribute("height", "1");
	    imageTag.setAttribute("class", "tracking");
	    imageTag.setAttribute("src",
	            "http://extra.evisitanalyst.com/at/tag.php?" + evastr0000);

	    document.body.appendChild(imageTag);
    }
};
