$(document).ready(function() {
	$("#topnav li").hoverIntent( megaHoverOver, megaHoverOut );
	
	if("source" in  urlParams)
	  $.cookie("source", urlParams['source'], { expires: 30 });	  

	if("utm_source" in  urlParams)
	  $.cookie("source", urlParams['utm_source'], { expires: 30 });	
	  
	if($.cookie("source") != null)
	{
		if($.cookie("source") == "webgains")
			$("#header .phones").hide();
	}
	
	if($.cookie("__utmz") != null && $.cookie("returningVis") == null ){
		blocks = $.cookie("__utmz").split(".");
		segments = blocks[blocks.length-1].split("|");
		for( i in segments ) {
			segment = segments[i];
			if( segment.indexOf('=') != -1 )
			{
				utm_key = segment.substring(0, segment.indexOf('=') );
				utm_val = segment.substring(segment.indexOf('=')+1 );
				
				if(utm_key == "utmcsr")
				{
					pageTracker._setCustomVar(2,"Original_Source",utm_val, 1);
					//$("body").append("set source to -"+utm_val);
				}

				if(utm_key == "utmctr")
				{
					pageTracker._setCustomVar(3,"Original_Keyword",utm_val, 1);
					//$("body").append("set keyword to -"+utm_val);
				}
				
				$.cookie("returningVis", true, { expires: 180 });	
					
			}
					
		}
		
		
	}
	
	
});

var urlParams = {};
(function () {
    var e,
        d = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); },
        q = window.location.search.substring(1),
        r = /([^&=]+)=?([^&]*)/g;

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();



function megaHoverOver(){
 $(this).find(".sub").css({'width' :'1024'}); 
       $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
      
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 
    if ( $(this).find(".row").length > 0 ) { //If row exists...
        var biggestRow = 0;	
        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });
        $(this).find(".sub").css({'width' :'1024'}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
    } else { //If row does not exist...
        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : '1024'}); //Set Width
    }
};
//On Hover Out
function megaHoverOut(){
//$(this).hide(); 
  $(this).find(".sub").stop().hide();//fadeTo('fast', 0, function() { //Fade to 0 opactiy
      //$(this).hide();  //after fading, hide it
  //});
}

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
