var $j = jQuery.noConflict();

$j(document).ready(function(){

	// Homepage Fader 
 	
 	$j("#hero_stage > div.slide").hide();
 	
	$j(".hero_tabs").tabs("#hero_stage > div.slide", {
		effect: 'fade',
		fadeInSpeed: 1000,
		fadeOutSpeed: 2000,
		rotate: true
	}).slideshow({ 
		autoplay: true, 
		interval: 8000, 
		clickable: false, 
		autopause: false 
	});
	
	// Disable homepage slideshow when user clicks a tab
	$j('.hero_tabs a').click(function(){
		$j('.hero_tabs').data('slideshow').stop();
		return false;
	});
	
	// Testimonials Fader 
	$j('.testimonial').cycle({
		timeout: 6000,
		fx: 'fade',
		cleartype:  1,
		random: 1
	});
	
	// Mobile Advertising Fader
	$j('.callout .rotate').cycle({
		timeout: 8000,
		fx: 'fade'
	});

	// Accordions 
	
	$j(".accordion").accordion({ 
		active: false,
		autoHeight: false,
		navigation: true, 
		collapsible: true,
		change: function() { 
			$j(this).find('h2').blur(); 
		}
	});
	
	// Content Toggle
	
	$j(".reveal").hide();
	
	$j("a.toggle").click(function() {
		$j("div.reveal").slideToggle("fast");
		$j(this).hide();
		return false;
	});

	//Enable placeholder content
		
	$j( "input, textarea" ).placehold({
		placeholderClassName: "something-temporary"
	});

	$j(".ie6 .promotions .stage.last").hover(
		function(){
			$("#target_audience select").css('visibility', 'hidden');
		},
		function(){
			$("#target_audience select").css('visibility','visible');
		}
	);

	$j(".promotions .stage").tooltip({
		predelay: 200,
		effect: 'slide',
		offset: [40, 0]
	}).dynamic({ bottom: { direction: 'down', bounce: true } });

   //Create Networks overlays
/*   
   $("a[rel]").overlay({
   	top: '25%',
	onBeforeLoad: function() {
		// grab wrapper element inside content
		var wrap = this.getOverlay().find(".contentWrap");
		// load the page specified in the trigger
		wrap.load(this.getTrigger().attr("href") + ' #network_profile');
	}

	});
*/	
}); //END document ready


// Networks grid

function GridBorders(_objclass){   	var obj = "." + _objclass;    $j(obj + " tr:last td").css('border-width','0px 1px 0px 0px');	$j(obj + " tr").find('td:last').css('border-width','0px 0px 1px 0px');	$j(obj + " tr:last").find('td:last').css('border-width','0px 0px 0px 0px');}



// promotions 
function viewPromo(){	
	var obj = $j(".PromotionsGrid tr td img");
	var popup = $j(".popPromo");
	var fill = "popMiddle";
    var xPad = 5;
	var yPad = 5;
	var xPosi;
    var yPosi;
	var utililty = "off"; //set to on to expose values of elements
	
    obj.removeAttr("alt");
    $j("body").append("<div id=\"popPromo\" class=\"\" ><div class=\"popArrow\">&nbsp;</div><div class=\"poptopcap\"><img src=\"/images/promos/popCornerTopLeft.gif\" alt=\"\" class=\"lc\" /><img src=\"/images/promos/popCornerTopRight.gif\" alt=\"\" class=\"rc\" /><div class=\"popmiddle\">&nbsp;</div><div class=\"clear\"></div></div><div class=\"popMiddle\"></div><div class=\"popbottomcap\"><img src=\"/images/promos/popCornerBottomLeft.gif\" alt=\"\" class=\"lc\" /><img src=\"/images/promos/popCornerBottomRight.gif\" alt=\"\" class=\"rc\" /><div class=\"popmiddle\">&nbsp;</div><div class=\"clear\"></div></div></div>");
	$j(".PromotionsGrid tr").find("td:last").attr("class","goleft");

	function autoPosi(e, obj, objwidth, objheight, coord) {
	        var offset = obj.offset();
		    if(coord == "x") {
                xPosi = offset.left + objwidth ;           
    	        return xPosi;
    	    } 
    	    else {
    	        yPosi = offset.top + 20 ;
	            return yPosi;
	        }
	}
	
	obj.hover(function(e){
	    var popup = $j("#popPromo");
		var objparent = $j(this).parent("td");
        objoffset = objparent.offset();
		var objwidth = objparent.width();
	    var objheight = objparent.height();
	    popouttext = objparent.find(".popouttext");
		
        popup.find(".popMiddle").html(popouttext.html());
        if(objparent.attr("class") == "goleft"){
            popup.attr("class","popleft");
            objwidth = -popup.width() + 10 ;
        } else { 
            popup.attr("class","other");
        }
        popup.css("top",autoPosi(e, objparent, objwidth, objheight, "y") + "px").css("left",autoPosi(e, objparent, objwidth, objheight, "x")  + "px").show();				
        },
	    function(){
		    var popup = $j("#popPromo");
            popup.hide();
            popup.find(".popMiddle").html("");
            //if (utililty == "on"){$j("#util").remove();}
        });	
		
};
// this function will be designed to place a element in the corner of the window, 
// or with the cursor,  displaying a list of key/value pairs passed in as an array
function eUtil(arr) {
    
    $j.each(obj, function(key, val) {
        
    });
};

/*var whitePaperDisplayed = false;
document.observe('dom:loaded', function(){
	
	$('lnkWhitePaper').observe('click', function(e) {
		e.stop();
		if(!whitePaperDisplayed)
		{
			whitePaperDisplayed = true;
			new Effect.BlindDown('whitePaper', {duration: .5});
		}
	});
	
	$('whitePaperCloseButton').observe('click', function(e) {
		e.stop();
		whitePaperDisplayed = false;
		new Effect.BlindUp('whitePaper', {duration: .5});
	});
	
	$('frmWhitePaper').observe('submit', function(e){
		e.stop();
		if(isValidEmail($('txtWhitePaperEmail').value) == false){
			//Effect.Shake('whitePaper');
             alert('A valid email is required.');
		} else {
			new Ajax.Request('/ajax/white-paper.php', {
				method: 'POST',
				parameters: $('frmWhitePaper').serialize(),
				onSuccess: function(transport){
					var response = transport.responseText.evalJSON();
					if(response.success != undefined){
						$('lnkWhitePaperDownload').href = response.file;
						$('lnkWhitePaperDownload').innerHTML = response.text;
						Effect.Fade('emailContainer', {
							duration: .5,
							afterFinish: function (obj) {
								new Effect.Appear('downloadContainer', {duration: .5})
							}
						});
					} else {
						if(response.validation != undefined){
							if(response.validation.email){
								//Effect.Shake('whitePaper');
                                  alert('A valid email is required.');
							}
						} else {
							alert('error');
						}
					}
				},
				onFailure: function(){
					alert('failure');
				}
			});
		}
	});
});*/


function isValidEmail(emailAddress) {
	if(/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(emailAddress) == false) {
		return false;
	} else {
		return true;
	}
}
