<!--
/////////////////////////////////////////////////
// Written by Paul Lo, organized on 8/27/08
/////////////////////////////////////////////////

/////////////////////////////////////////////////
// JQuery Section - predefined
/////////////////////////////////////////////////
var $j = jQuery;
$j(document).ready(function()
{ 
   var mouseX;
	var mouseY;
	
	//-------- get mouse position --------
	$j().mousemove(function(e)
	{
	   mouseX = e.pageX;
	   mouseY = e.pageY;
   });     
   
   //-------- google tracking --------
   $j("a,input.common-tracking").click(function()
   {
      var id = $j(this).attr("id");
      var trackingUrl = "";
      var parentId = "";
      
      if($j(this).parent().length > 0)
      {
         parentId = $j(this).parent().attr("id");
      }
      
      if($j(this).hasClass("wwo-url")) // country tracking
      {
         var loc = $j(this).children("span");
         if(loc.length > 0)
         {
            trackingUrl = "/corporate/worldwideoffices/location/" + loc.text();
         }
      }
      
      if(trackingUrl.length > 0)
      {
         pageTracker._trackPageview(trackingUrl);
      }
   });
   
   //---------------Master Page----------------------   
   $j(".list-tabs").mouseenter(function()
   {
      var li = $j(this);
      var tab = $j(this).children("a");
      //var li = $j(this).parent(".list-tabs");
      //var tab = $j(this);
      tab.addClass("on");
      
      setTimeout(function()
      {
			if(tab.hasClass("on"))
			{
				var content = li.find("div.top-nav-content-wrapper");
				if(content.length > 0 && content.filter(":animated").length == 0)
				{
				   var width = content.width();
				   var height = content.height();
				   content.css({width:width+"px",height:height+"px"});
				   
					content.slideDown(250);
				}
			}
      }, 300);
   }).mouseleave(function()
   {
      //var tab = $j(this);
      //tab.removeClass("on");
      var li = $j(this);
      var tab = $j(this).children("a");
      tab.removeClass("on");
   
      setTimeout(function()
      {  
         if(!tab.hasClass("on"))
         {
				var content = li.find("div.top-nav-content-wrapper");
				if(content.length > 0 && content.css("display") != 'none')
				{
					content.slideUp(250);
				}
			}
      }, 150);
   });
   
   //---------------Prodict Page----------------------
   // content overlay close button
   $j("a.contentOverlayCloseBtn").click(function()
   {
      if(!$j(this).hasClass("link"))
      {
         ContentOverlay.Close();
      }
   });
    
   //----------- general functions -----------
   // product selector link
   // rel = url, a.productSelectorLink
   $j("select.ddl-link").change(function()
   {
      var url = $j(this).attr("rel");
      
      var exUrl = $j(this.options[this.selectedIndex]).attr("rel");
      
      if(typeof(exUrl) != 'undefined' && exUrl.length > 0)
         url = exUrl;
      
      var val = this.options[this.selectedIndex].value;
      
      if(val != null && val != -1)
      {
         location.href = url + val;
      }
   });
   
   $j("a.frameUrl, input.frameUrl").click(function()
   {
      var url = $j(this).attr("rel");
      ContentOverlay.ShowProductSelector(url);
            
      return false;
   });
   
   // all Tree Node Sign Click
   $j("div.treeExpandSign > a").click(function()
   {
      var treeTitle = $j(this).parent().next();
      if(treeTitle != null)
         $j(treeTitle).click();
   });
   
   // all Neutral(no content, downloads only) attributes addon
   $j.each($j("div.treeNeutralSign > a"), function()
   {
      var treeTitle = $j(this).parent().next();
      if(treeTitle != null)
      {
         var href = $j(treeTitle).attr("href");
         var target = $j(treeTitle).attr("target");
         
         // if resource only has downloadable files, fill in href & target
         // attrs, or else if resource is a clickable, then add the click
         // event to the current element
         if(typeof(href) == 'undefined' || href == null || href.length == 0)
         {
            $j(this).click(function()
            {
               if($j(treeTitle).click)
                  $j(treeTitle).click();
            });
         }
         else
         {
             $j(this).attr("href", href);
             $j(this).attr("target", target);
         }
      }
   });
   
   // all Tree Nodes Click
   // <a> with rel='treeview_link' with a <div> as
   // immediate sibling for the content
   $j("a[rel='treeview_link']").click(function()
      {
         var sectionToShow = $j(this).parent().next();
         var signSpan = $j(this).prev();
         
         if($j(signSpan).hasClass("treeExpandSign"))
         {
             // <a>
             var signLink = signSpan.children(":first");
             
             if($j.trim($j(signLink).html()) == "+")
             {       
                $j(signLink).html("-");
                $j(signLink).css({marginLeft: "2px"});
             }
             else
             {
                $j(signLink).html("+");    
                $j(signLink).css({marginLeft: ""});  
             }
             
             $j(sectionToShow).slideToggle(300);
         }
      });
  
   //----------- company press template -----------
   $j(".supportContent").click(function()
   {
      var rel = $j(this).attr("rel");
      var title = $j(this).attr("title");
      var id = $j(this).attr("id");
      var hasFrame = $j(this).hasClass("frameContent");
      var hasSearch = $j(this).hasClass("searchContent");
      
      if(typeof(rel) != 'undefined' && rel.length > 0)
      {
         var contentId = "#" + id + "_content"; 
         
         $j(this).siblings("a.linkOn").removeClass("linkOn");
         
         if(!$j(this).hasClass("linkOn"))
            $j(this).addClass("linkOn");
          
         if(hasFrame)
         {
            contentId = "#frame_content";
            var src = $j(this).attr("url");
            
            if(hasSearch)
            {
               var term = $j(this).prev("input").val();
               src = src + term;
            }
            
            $j(contentId).children("iframe").attr("src", src);
         }
         
         var contentBody = $j("#" + rel + " div.company-landing");
         
         if(contentBody.css("display") != 'block' || contentBody.css("display") == '')
         {
            contentBody.slideDown(500, function()
            {
					if($j(contentId).css("display") != 'block')
					{
						$j(contentId).siblings().css("display", "none");
						$j(contentId).fadeIn(300, function()
						{
						   
						});
					}
            });
         }
         else
         {
            if($j(contentId).css("display") != 'block')
				{
					$j(contentId).siblings().css("display", "none");
					$j(contentId).fadeIn(300, function()
					{
					   
					});
				}
         }
         
         return false;
      }
   });

   //----------- press inquiry page -----------
   $j("a.pi-item").click(function()
   {
      var html = $j(this).html();
      $j("#prContentHeader").html(html);
       
      // update comment header text
      var cmtHeader = $j(this).attr("cmtText");
      $j("#prCmtText").html(cmtHeader);
      
      // update pr type
      $j("#templateRightContent").next("input[type='hidden']").val(html);
   });
    
   //----------- all forms client check -----------
   function FormInputEmptyCheck(formid, rel, fieldname)
   {
      // textbox or textarea
      var ctrl = $j("#" + formid + " *[rel='" + rel + "']");
      var val = ctrl.val();
       
      if(typeof(val) == 'undefined' || val.length == 0) // input box
      {
         alert("Please fill in " + fieldname);
         return false;
      } 
       
      return true; 
   }
    
   function FormCheckBoxCheck(formid, rel, msg)
   {
      var ctrl = $j("#" + formid + " input[rel='" + rel + "']");
      var checked = ctrl.attr("checked");
      
      if(!checked)
      {
         alert(msg);
         return false;
      } 
       
      return true; 
   }
   
   // form on /contactus/feedback/ 
   $j("*.feedback-submit").click(function()
   {
      return (FormInputEmptyCheck("feedBackForm", "email", "email") && 
              FormCheckBoxCheck("feedBackForm", "terms", "You must agree to the D-Link's terms and conditions to submit your feedback."));
   });
    
   //----------- /support/supportscope/ page ----------- 
   $j("*.dynamic-swap").click(function()
   {
      var rel = $j(this).attr("rel");
       
      $j("#" + rel).siblings("*[rel='swapContent']").slideUp(300, function()
      {
         $j("#" + rel).slideDown(500);
      });
   });
   
   //
});
/////////////////////////////////////////////////
// JQuery Section - predefined end
/////////////////////////////////////////////////

//////////////////////////////////////////////////
// Common Functions
//////////////////////////////////////////////////
function LoadSwappedIllust()
{
	$j(document).ready(function()
	{     
		var swapIllust = $j("#swappedIllust");
		var illust = $j("#sec_illustration");
		
		if(illust.length > 0 && swapIllust.length > 0)
		{
			var style = illust.attr("style");
			swapIllust.attr("style", style); // copy over the style
		  
			var bkImg = swapIllust.css("background-image");
			var indOfExt = bkImg.lastIndexOf(".");
			var path = bkImg.substring(0, indOfExt) + "_over" + bkImg.substring(indOfExt);
			swapIllust.css({"background-image" : path});
		}
   });
}

function LoadOverlayBlock(id, idToAppend, width, height, positions, direction, speed)
{
   $j(document).ready(function()
	{
	    if(positions != null)
	    {
	       var pos = "";
	       var op = 0.33;
	       //var speed = 700;
	       if(speed == null)
	          speed = 700;
	       
	       if(positions.bottom != null)
	       {
	          pos += "bottom:" + positions.bottom + "px;";
	       }
	       
	       if(positions.top != null)
	       {
	          pos += "top:" + positions.top + "px;";
	       }
	       
	       if(positions.left != null)
	       {
	          pos += "left:" + positions.left + "px;";
	       }
	       
	       if(positions.right != null)
	       {
	          pos += "right:" + positions.right + "px;";
	       }
	       
	       if(idToAppend.indexOf("$") == -1) // $ not found, so it's a tag name, or else it's an id
	          idToAppend = "#" + idToAppend;
	       else
	          idToAppend = idToAppend.replace("$", "");

			 $j(idToAppend).append("<div id=\"" + id + "_shadow\" style=\"width:" + width + ";position:absolute;z-index:9999;height:" + height + "px;overflow:hidden;" + pos + "\"><div id=\"" + id + "\" style=\"position:relative;background-color:black;\"><!-- ie6 --></div></div>");
			 
			 if(direction != null )
			 {
				 if(direction == "up")
				 {
					 $j("#" + id).fadeTo(speed, op).css({height: height + "px",bottom:"-" + height + "px"}).animate({bottom:"0px"}, "slow");
				 }
				 else if(direction == "down")
				 {
				    $j("#" + id).fadeTo(speed, op).css({height: height + "px",top:"-" + height + "px"}).animate({top:"0px"}, "slow");
				 }
				 else if(direction == "right")
				 {
				    $j("#" + id).fadeTo(speed, op).css({height: height + "px",left:"-" + width}).animate({left:"0px"}, "slow");
				 }
				 else if(direction == "left")
				 {
				    $j("#" + id).fadeTo(speed, op).css({height: height + "px",right:"-" + width}).animate({right:"0px"}, "slow");
				 }
			 }
	    }
	});
}
//////////////////////////////////////////////////
// Close HomePage Setting @ Default page
//////////////////////////////////////////////////
function generateCommonFlash(id, url, width, height, params, flashvars, attributes, showAlternative)
{      
   if(showAlternative == null)
      showAlternative = false;

   if(params == null)
      params = {};

   /* common params */
   params.allowScriptAccess = "sameDomain";
   params.quality = "high";
   params.bgcolor = "#ffffff";
   params.wmode = "transparent";
   
   var exIntall = null;
   
   if(!showAlternative)
      exIntall = "/expressInstall.swf";
   
   swfobject.embedSWF(url, id, width, height, "9.0.0", exIntall, flashvars, params, attributes);
}

//----------- D-Link Home Page Cookie Setting -----------
var cookieHPValues = {HHO:'1', SMB:'2', LB:'3', GOV:'4', Reseller: '5'};
var cookieHPPair = $H();

// cookie settings
var cookiePkg = 
{
   name: 'dlinkHPSetting',
   value: cookieHPValues.HHO,
   expDays: 365,
   expires: 3600000 * 24 * this.expDays
};

function setHPCookie(value, pkg)
{
   if(value != null)
      pkg.value = value;
   
   var exDate = new Date();
   exDate.setDate(exDate.getDate() + pkg.expDays);
   
   document.cookie = pkg.name + "=" + escape(pkg.value) + ";expires=" + exDate.toGMTString() + ";path=/";
}

function getHPCookie(cookieName)
{
   if (document.cookie.length>0)
   {
      var start = document.cookie.indexOf(cookieName + "=");
      if (start!=-1)
      { 
         start = start + cookieName.length+1; 
         end = document.cookie.indexOf(";", start);
         if (end==-1) end=document.cookie.length;
            return unescape(document.cookie.substring(start,end));
      } 
   }
   return "";
}

//called when the check box on each tab is clicked, and
//set the cookie; if unchecked, set HHO as Default
function checkHP(control, value, pkg)
{
   if(control != null && pkg != null)
   {
      if(value == null)
         value = cookieHPValues.HHO;
         
      if(control.checked)
      {
         setHPCookie(value, pkg);
         
         // uncheck all other options
         cookieHPPair.each(function(pair)
         {
            var ctrl = $(pair.key);
            if(ctrl.id != control.id)
            {
               ctrl.checked = false;
            }
         }.bind(this));
      }
      else
      {
         setHPCookie(cookieHPValues.HHO, pkg);
      }
   }
}

// check if the user key in numbers
function isNumberKey(evt)
{
   var charCode = (evt.which) ? evt.which : event.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

   return true;
}

// make dynamic data be able to perform actions
function FrameUrl(element)
{
   $j(document).ready(function()
   { 
      var url = $j(element).attr("rel");
      
      // kb tracking for search box
      if($j(element).hasClass("support-read"))
      {
         if(typeof(url) != 'undefined' && url.length > 0)
         {
            pageTracker._trackPageview(url);
         }
      }
      
		ContentOverlay.ShowProductSelector(url);
		return false;
   });
}

// make search result list accessible from keyboard with up & down & enter
function keyboardDown(e, resultId)
{
   var keynum;
   var result = $(resultId);
   var isSelected = false;
   var resultList = result.down(0); // ul
   var tb = Event.element(e);
   
   if(result == null)
      return;

   // get keycode for different browser
   if(window.event) // IE
   {
      keynum = e.keyCode;
   }
   else if(e.which) // Netscape/Firefox/Opera
   {
      keynum = e.which;
   }
   
   
   if(keynum == 40 && result != null && result.hasClassName('on')) // key arrow down
   { 
      resultList.childElements().each(function(element, index)
      {
         if($(element).hasClassName('selected'))
         {				    
            if(index < resultList.childElements().length - 1)
            {
               $(element).removeClassName('selected');
               $(element).next(0).addClassName('selected');               
            }
            
            result.scrollTop = result.scrollTop + 16;
            
            isSelected = true;
            throw $break;
         }
      });
      
      if(!isSelected)
         resultList.down(0).addClassName('selected');
   }
   else if(keynum == 38 && result != null && result.hasClassName('on')) // key arrow up
   {
      resultList.childElements().each(function(element, index)
      {
         if($(element).hasClassName('selected'))
         {
            if(index > 0)
            {
               $(element).removeClassName('selected');
               $(element).previous(0).addClassName('selected');
            }
            else
            {
               $(element).removeClassName('selected');
            }
            
            if(result.scrollTop >= 16)
            {
               result.scrollTop = result.scrollTop - 16;
            }
            else if(result.scrollTop > 0 && result.scrollTop < 16)
            {
               result.scrollTop = 0;
            }
            
            isSelected = true;
            throw $break;
         }
      });
      
      if(!isSelected)
         resultList.up(0).addClassName('selected');
   }
}
//////////////////////////////////////////////////
//  Product Selector functions
//////////////////////////////////////////////////
var ContentOverlay =
{
   overlaySection: "#contentOverlaySection",
   Open: function() 
   {
      $j(this.overlaySection).show(500, function()
      {
         var illustration = $j("div.categoryIllustration");
         var background = illustration.css("background");
         
         if(illustration.length > 0 && background != null && typeof(background) != 'undefined' && illustration.css("background").length > 0)
            illustration.hide();
      });
   },
   Close: function() 
   {
      var overlaySec = this.overlaySection;
      //$j(this.overlaySection).hide(500);
      var illustration = $j("div.categoryIllustration");
      if(illustration.length > 0 && illustration.css("display") != 'block')
      {
         illustration.show(10, function()
         {
            $j(overlaySec).hide(500);
         });
      }
      else
      {
         $j(overlaySec).hide(500);
      }

      // make frame url empty
      this.ShowProductSelector(null);
   },
   HideSiblings: function(sectionId)
   {
      var sec = $j("#" + sectionId);
      sec.siblings(":not(a)").hide();
      sec.show();
   },
   ShowProductSelector: function(url)
   {
      if(url != null && url.length > 0)
      {
         this.Open();
      }
      
      if(url == null)
         url = '';
      
      this.HideSiblings("productSelectorSection");
      $j("#productSelectorFrame").attr("src", url);
   },
   ShowProductFAQ: function(sectionId, faqId)
   {
      if($j("#" + faqId).length > 0)
      {
         this.Open();
         this.HideSiblings(sectionId);
         this.HideSiblings(faqId);
         $j("#" + faqId).slideDown(300);
      }
   },
   ShowGeneralSection: function(sectionId)
   {
      this.Open();
      this.HideSiblings(sectionId);
   }
};

//----------- pop up for retailer map ----------
function popup(width,height,url,name)
{
   window.open(url,name,"width=" + width + ",height=" + height + ",toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1");
}

function mapPopup(url)
{
   popup(900,520,url,"Map");
}

// don't put the whole path, just the value, e.g. pid
function valueUrlEncode(url)
{
   var reserved = new Array("$", "&", "+", ",", "/", ":", ";", "=", "?", "@");
   var coded = new Array("%24", "%26", "%2b", "%2c", "%2f", "%3a", "%3b", "%3d", "%3f", "%40");
   
   for(var i = 0; i < reserved.length; i++)
   {
      url = url.replace(reserved[i], coded[i]);
   }
   return unescape(url);
}

// End DLink.js
//////////////////////////////////////////////////
// -->
