﻿// Main Navigation Scripts
var $j = jQuery.noConflict();
$j(document).ready(function(){
    ImageRoll.rollover.init(".mainNavigationChild .tab","img.maintabimage");    
    $j("img.maintabimage").removeAttr("alt");
    $j(".mainNavigationChild .tab").hover(function(){openpanel(this, 0)},function(){closepanel(this, 300)});
    $j(".mainNavigationChild .tab").mousemove(function(){openpanel(this, 0);});
    function openpanel(sender, delay){if($j(sender).find('.submenu').not(':animated').is(':hidden')){$j(sender).find('.submenu').animate({opacity: 1.0}, delay).show();}}
    function closepanel(sender, delay){$j(sender).find('.submenu').animate({opacity: 1.0}, delay).hide();}
});   

ImageRoll = {};
ImageRoll.rollover = 
{
   init: function(trigger,target)
   {
    this.preload(trigger,target); 
      $j(trigger).hover(
         function () { $j(this).find(target).attr( 'src', ImageRoll.rollover.newimage($j(this).find(target).attr('src')) ); }, 
         function () { $j(this).find(target).attr( 'src', ImageRoll.rollover.oldimage($j(this).find(target).attr('src')) ); }
      );
   },
   preload: function(trigger,target)
   {
      $j(window).bind('load', function() {
         $j(trigger + " " + target).each( function( key, elm ) { $j('<img>').attr( 'src', ImageRoll.rollover.newimage( $j(this).attr('src') ) ); });
      });
   },
   newimage: function( src )
   { 
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_o' + src.match(/(\.[a-z]+)$/)[0]; 
   },
   oldimage: function( src )
   { 
      return src.replace(/_o\./, '.'); 
   }
};


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;
	}
}