// CSI INCLUDE FOR HEADER AND FOOTER FILES
// JS file developed in support to convert all SSI Includes to a CSI Include using javascript
// javascript to read in two paramaters within the <div id="prim_sec">, based on the parameters, 
// the appropriate header file is loaded and then the appropriate secondary navigation link is 
// highlighted. If nothing is passed (no div exists), the header_main.htm is loaded by default.

// 082409: [bern} reduced header files to a single header.htm
//         no longer need ANY parameter for header.htm to load
//         added "none" case statement for pages that don't need a header
//         added interaction functionality for rebrand of header (pull-downs, language rollovers, bgiframe, sign in URI

// 090109: [bern] added if else statement to check if header already exists (hardcoded). 
//         if it doesn't exist, then it loads the header file.
//         if it does exist, then it applys the correct interaction by calling headerCallback();
//         hardcoding mainly done on outside vendor or application sites (forumns, OMS, investor, press, etc)
  
// 090309: [bern] 
//         ADDED THREE NEW FUNCTIONS
//           loadFooter() : load the footer when the <div "includeFooter">/csi/footer.htm</div> is in the page 
//           convertToAbsolute() : converts HF links, IMG src, FORM action, INPUT src to absolute URIs
//           convertToProxy() : prepends IMG src and INPUT src with /xilinx_HF for proxy servers
//         ADDED TWO NEW SWITCH CASES
//           absolute: used for sites like Registration and Search that can load the header using a relative link,
//                     but still need the links and images to be absolute
//           proxy: used for sites on DMZ like xapps that will use a proxy server to load the header, links need
//                  to be absolute and the images need /xilinx_HF prepended

// 091009: [bern]
//         updated convertToAbsolute(): IE6 adds the domain to the HREF so finding HREF's starting with "/" wasn't working
//            IE6 HREFs ended up being "http://testlinx" or whatever domain you were on


// 091409: [bern]
//         updated convertToProxy(): fixed IE security pop-up by converting IMG src to /xilinx_HF
//         first; moved changing URLs to absolute w/in function; added browser detection

// 111009: [bern]
//		   added language pulldown functionality - switching languages changes the domain, but keeps the same URL
//         added sign-in slide down panel

$(document).ready(function(){
function loadFooter() {
	var divFooter = 'div#includeFooter';
	var footerURL = $(divFooter).text();
	if (footerURL.indexOf("csi") != -1)
	// something.indexOf("keyword") returns -1 when keyword doesn't exist
	// only load footer if a URL is passed - all footer files will be placed in csi directory
	{	
		$('#loadfooter').load(footerURL);
	} //end if

}
function headerCallback() {
		$("ul.lang-menu li:first").hover(
			function () {
				$(this).children("ul").addClass('xlanghover');
			},function(){
				$(this).children("ul").removeClass('xlanghover');	
			});//hover
		$('ul#languageSelect a img').hover(function() {
		   var newSrc = $(this).attr('src').replace('_s.gif','_h.gif');
		   $(this).attr('src',newSrc);
		}, function() {
			var oldSrc = $(this).attr('src').replace('_h.gif','_s.gif');
			$(this).attr('src',oldSrc);
		});
 	
		$('ul#languageSelect a').click(function() {  // swaps out domain of the current URL
			var domain = document.domain; 
            var currentURL = window.location.href;
   			var protocol = window.location.protocol;
    		var hrefVal = protocol + '//' + domain;
    		var ie = $.browser.msie; 
			var langHREF = $(this).attr("href");
           if (domain == 'www.xilinx.com' || domain == 'japan.xilinx.com' || domain == 'china.xilinx.com') {
						var newHref =  currentURL.replace(hrefVal, langHREF);	
						$(this).attr('href', newHref);
			} // if its any other domain, then it defaults to the homepage.
		});

	    $('li.mainItem div.megaDropdown').bgiframe(); /* Changed by Infosys for using bgiframe for navigation bar */
		$("li.mainItem").hover( 
			function () {
				$(this).addClass('addArrowOnHover'); /* Added by Infosys for showing red arrow on hover in navigation bar */
				$(this).children('a.menuTitle').addClass('titleRed');
				$(this).children("div.megaDropdown").addClass('xmenuhover');
			},function(){
				$(this).removeClass('addArrowOnHover'); /* Added by Infosys for removing red arrow on hover in navigation bar */
				$(this).children('a.menuTitle').removeClass('titleRed');
				$(this).children("div.megaDropdown").removeClass('xmenuhover');	
			});//hover /* Changed by Infosys for using bgiframe for navigation bar */
		
		$('li.mainItem:last').css('margin','0 0 0 0'); /* Changed by Infosys for using bgiframe for navigation bar */
		$('#ikeyword').focus(function () {
		   $(this).attr('value','');
		});

		var langHeader= $(".xheader").attr("id");
        if ( $('#signinAcct').length == 0 ) {
			if ( document.cookie.indexOf("iPlanetDirectoryPro=") == -1) {
			   switch(langHeader)
					{								
						case "JPheader":
							$('#signIn').html("<a href='" + dst_url + "'>\u30b5\u30a4\u30f3\u30a4\u30f3<\/a>");
							break;
						case "CNheader":
							$('#signIn').html("<a href='" + dst_url + "'>\u767b\u5f55\u5e76\u8bbf\u95ee\u5e10\u6237<\/a>");
							break;	
						default:  
							$('#signIn').html("<a href='" + dst_url + "'>Sign in<\/a>");
					} // end switch
			} else {
			   switch(langHeader)
					{								
						case "JPheader":
							$('#signIn').html("<a href='" + dst_url + "'>\u30de\u30a4 \u30a2\u30ab\u30a6\u30f3\u30c8<\/a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='https:\/\/" + dst_hostname + "\/webreg\/logout.do'>\u30b5\u30a4\u30f3\u30a2\u30a6\u30c8<\/a>");									break;
						case "CNheader":
							$('#signIn').html("<a href='" + dst_url + "'>\u6211\u7684\u5e10\u6237<\/a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='https:\/\/" + dst_hostname + "\/webreg\/logout.do'>\u9000\u51fa<\/a>");									break;	
						default:  
							$('#signIn').html("<a href='" + dst_url + "'>My Account<\/a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='https:\/\/" + dst_hostname + "\/webreg\/logout.do'>Sign Out<\/a>");
					} // end switch
			} // end else
        } // end if signin acct does not exist

	  if ( $('#signinAcct').length == 1 ) {
			if (document.cookie.indexOf("iPlanetDirectoryPro=") == -1)  
			{	
			   $('#signinAcct form').attr('action', dst_login);
			   $('#pwdReset').attr('href', dst_pwdReset);
			   $('#createAcct').attr('href', dst_createAcct);
           switch(langHeader)
					{								
						case "JPheader":                       
							$('#signIn').html("<a href='#' id='open-panel'>\u30b5\u30a4\u30f3\u30a4\u30f3<\/a>");
							break;
						case "CNheader":						
							$('#signIn').html("<a href=\"#\" id=\"open-panel\">\u767b\u5f55\u5e76\u8bbf\u95ee\u5e10\u6237<\/a>");
							break;	
						default:                         						
							$('#signIn').html("<a href='#' id='open-panel'>Sign in<\/a>");
					} // end switch
		} else {
			
			 switch(langHeader)
				{								
					case "JPheader":
						$('#signIn').html("<a href='" + dst_myAcct + "'>\u30de\u30a4 \u30a2\u30ab\u30a6\u30f3\u30c8<\/a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='https:\/\/" + dst_hostname + "\/webreg\/logout.do'>\u30b5\u30a4\u30f3\u30a2\u30a6\u30c8<\/a>");
						break;
					case "CNheader":
						$('#signIn').html("<a href='" + dst_myAcct + "'>\u6211\u7684\u5e10\u6237<\/a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='https:\/\/" + dst_hostname + "\/webreg\/logout.do'>\u9000\u51fa<\/a>");									
						break;	
					default:  
						$('#signIn').html("<a href='" + dst_myAcct + "'>My Account<\/a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='https:\/\/" + dst_hostname + "\/webreg\/logout.do'>Sign Out<\/a>");
				} // end switch
		} // end else
	} // end if signin acct does exist
 $("#open-panel").click(function(){       
			$("div#panel").slideDown("slow");
            return false;
		});	
		 $("#close-panel").click(function(){
			$("div#panel").slideUp("slow");	
            return false;
		});	
} // end function headercallback()
function convertToAbsolute() {
    var domain = document.domain; // need this variable for the IE functions
    var protocol = window.location.protocol;
    var hrefVal = protocol + '//' + domain;
    var ie = $.browser.msie; 

    if (ie) {
		$('.xheader img[@src^='+ hrefVal +']').each(function() { // changes SRC to absolute for images in header
 			var oldSrc = $(this).attr('src').replace(hrefVal,'http://www.xilinx.com');
       		$(this).attr('src',oldSrc);
		});
		$('.xheader a[@href^='+ hrefVal +']').each(function() { // changes header links to absolute
			var oldHref = $(this).attr('href').replace(hrefVal,'http://www.xilinx.com');
      		$(this).attr('href',oldHref);
    	});
		$('.xheader input#isearch[@src^='+ hrefVal +']').each(function() { // changes SRC to absolute for submit button
 			var oldSrc = $(this).attr('src').replace(hrefVal,'http://www.xilinx.com');
       		$(this).attr('src',oldSrc);
		});  
		$('#footer a[@href^='+ hrefVal +']').each(function() { // changes footer links to absolute
			var oldHref = $(this).attr('href').replace(hrefVal,'http://www.xilinx.com');
        	$(this).attr('href',oldHref);
   		});
		$('#footer img[@src^='+ hrefVal +']').each(function() { // changes SRC to absolute for images in footer
 			var oldSrc = $(this).attr('src').replace(hrefVal,'http://www.xilinx.com');
       		$(this).attr('src',oldSrc);
		});  
		$('.xheader form[@action^='+ hrefVal +']').each(function() { // changes SRC to absolute for search
            var originalAction = $(this).attr('action').replace(hrefVal,'http://www.xilinx.com');
            $(this).attr('action',originalAction);
        });

    } else {
		$('.xheader img[@src^=\/]').each(function() { // changes SRC to absolute for images in header
			var originalSrc = $(this).attr('src');
			$(this).attr('src', 'http://www.xilinx.com' + originalSrc);
		});
		
		$('.xheader a[@href^=\/]').each(function() { // changes header links to absolute
			var originalHref= $(this).attr('href');       
			$(this).attr('href', 'http://www.xilinx.com' + originalHref);
		});   
		
		$('.xheader input#isearch[@src^=\/]').each(function() { // changes SRC to absolute for submit button
			var originalSrc = $(this).attr('src');
			$(this).attr('src', 'http://www.xilinx.com' + originalSrc);
		});
		$('#footer a[@href^=\/]').each(function() { // changes footer links to absolute
			var originalHref = $(this).attr('href');
			$(this).attr('href', 'http://www.xilinx.com' + originalHref);
		});
		$('#footer img[@src^=\/]').each(function() { // changes SRC to absolute for images in header
			var originalSrc = $(this).attr('src');
			$(this).attr('src', 'http://www.xilinx.com' + originalSrc);
		});
        $('.xheader form').each(function() { // changes SRC to absolute for submit button
			var originalAction = $(this).attr('action');
			$(this).attr('action', 'http://www.xilinx.com' + originalAction);
		});
    } // end else

	

    
	




 } // end convertToAbsolute()
function convertToProxy() 
{
    var domain   = document.domain; // need this variable for the IE functions
    var protocol = window.location.protocol;
    var hrefVal  = protocol + '//' + domain;
    var ie = $.browser.msie; 

    if (ie || (domain=='xapps11.xilinx.com') || (domain=='tapps11.xilinx.com')) {
        $('.xheader img[@src^='+ hrefVal +']').each(function() { // changes SRC to absolute for images in header
            var oldSrc = $(this).attr('src').replace(hrefVal,'/xilinx_HF');
            $(this).attr('src',oldSrc);
        });

        $('.xheader input#isearch[@src^='+ hrefVal +']').each(function() { // changes SRC to absolute for submit button
            var oldSrc = $(this).attr('src').replace(hrefVal,'/xilinx_HF');
            $(this).attr('src',oldSrc);
        });  
		$('#footer img[@src^='+ hrefVal +']').each(function() { // changes SRC to absolute for images in footer
 			var oldSrc = $(this).attr('src').replace(hrefVal,'/xilinx_HF');
       		$(this).attr('src',oldSrc);
		});  

        $('.xheader a[@href^='+ hrefVal +']').each(function() { // changes header links to absolute
            var oldHref = $(this).attr('href').replace(hrefVal,'http://www.xilinx.com');
            $(this).attr('href',oldHref);
        });

        $('#footer a[@href^='+ hrefVal +']').each(function() { // changes footer links to absolute
            var oldHref = $(this).attr('href').replace(hrefVal,'http://www.xilinx.com');
            $(this).attr('href',oldHref);
        });
        $('.xheader form[@action^='+ hrefVal +']').each(function() { // changes SRC to absolute for search
            var originalAction = $(this).attr('action').replace(hrefVal,'http://www.xilinx.com');
            $(this).attr('action',originalAction);
        });


    } else { // for all other browsers

        $('.xheader img[@src^=\/]').each(function() { // changes SRC to absolute for images in header
            var originalSrc = $(this).attr('src');
            $(this).attr('src', '/xilinx_HF' + originalSrc);
        });

        $('.xheader input#isearch[@src^=\/]').each(function() { // changes SRC to absolute for submit button
            var originalSrc = $(this).attr('src');
            $(this).attr('src', '/xilinx_HF' + originalSrc);
        });
        $('#footer img[@src^=\/]').each(function() { // changes SRC to absolute for images in footer
			var originalSrc = $(this).attr('src');
			$(this).attr('src', '/xilinx_HF' + originalSrc);
		});

        $('.xheader a[@href^=\/]').each(function() { // changes header links to absolute
            var originalHref= $(this).attr('href');       
            $(this).attr('href', 'http://www.xilinx.com' + originalHref);
        });   

        $('#footer a[@href^=\/]').each(function() { // changes footer links to absolute
            var originalHref = $(this).attr('href');
            $(this).attr('href', 'http://www.xilinx.com' + originalHref);
        });
        $('.xheader form').each(function() { // changes SRC to absolute for submit button
     	   var originalAction = $(this).attr('action');
   	     $(this).attr('action', 'http://www.xilinx.com' + originalAction);
  		});
    } // end else

	if ((domain=='xapps9.xilinx.com') || (domain=='tapps8.xilinx.com')) {
		$('.xheader form').each(function() { // changes SRC to absolute for submit button
				var originalAction = $(this).attr('action');
				$(this).attr('action', 'http://www.xilinx.com' + originalAction);
		});
	}

} // end convertToProxy()

if ( $('.xheader').length > 0 ) { // header exists; hardcoded into page
    headerCallback();
} else { // header does not exist; load into page
    $('#prim_sec').hide();
	$('#includeFooter').hide();
    var psText = $('#prim_sec').text(); // grab text within div#prim_sec
	var psList = psText.split('|'); // parse text
	var psNumItems = $(psList).length; // capture number of items passed
	var primary = psList[0]; // primary navigation text
	var secondary = psList[1]; // secondary navigation text
	$('body')
		.prepend('<div id="loadheader"></div>') 
		// adds new div#loadheader right after the <body> tag; div used to load header file content into 
		.append('<div id="loadfooter"></div>'); 
		// adds new div#loadfooter right before the </body> tag; div used to load footer file content into 

	var loadHeader = $('#loadheader');
    switch(primary)
	{
		case "Main_Korean": 
			$(loadHeader).load('/csi/header_main_korean.htm', function() {
					headerCallback();
				});
			break;
		case "Main_TraditionalCns": 
			$(loadHeader).load('/csi/header_main_traditionalcns.htm', function() {
					headerCallback();
				});
			break;
        case "none": 			
			break;
        case "absolute": 
            loadFooter();
			$(loadHeader).load('/csi/header.htm', function() {
				convertToAbsolute();
				headerCallback();
			});
							
			break;
        case "proxy":
			$('#loadfooter').load('/xilinx_HF/csi/footer.htm');
			$(loadHeader).load('/xilinx_HF/csi/header.htm', function() {
				convertToProxy();
				headerCallback();
			});
			break;
       
		default: 
			$(loadHeader).load('/csi/header.htm', function() {			
				headerCallback();
			});
			loadFooter();	
      } // end of switch
}		
});


// Author: Ramola Gokhale
// For Sign In / My Account
devInt = new Array ("xdtestlinx", "xdstagelinx", "www.stage.xd.xilinx.com", "www.preview.xd.xilinx.com", 
                    "xitestlinx", "xistagelinx", "www.stage.xi.xilinx.com", "www.preview.xi.xilinx.com",
                    "xdtargetj", "xdstagelinxj", "japan.stage.xd.xilinx.com", "japan.preview.xd.xilinx.com",
                    "xitargetj", "xistagelinxj", "japan.stage.xi.xilinx.com", "japan.preview.xi.xilinx.com",
                    "xdcns-target", "xdcns-stagelinx", "china.stage.xd.xilinx.com", "china.preview.xd.xilinx.com",
                    "xicns-target", "xicns-stagelinx", "china.stage.xi.xilinx.com", "china.preview.xi.xilinx.com",
                    "xisecure", "secure.xi");

uatPub = new Array ("xttestlinx", "xtstagelinx", "www.stage.xt.xilinx.com", "www.preview.xt.xilinx.com",
                    "xttargetj", "xtstagelinxj", "japan.stage.xt.xilinx.com", "japan.preview.xt.xilinx.com",
                    "xtcns-target", "xtcns-stagelinx", "china.stage.xt.xilinx.com", "china.preview.xt.xilinx.com",
                    "xtsecure", "secure.xt",
                    "xptestlinx", "xpstagelinx", "testlinx", "stagelinx", "www.stage.xilinx.com", "www.preview.xilinx.com",
                    "xptargetj", "xpstagelinxj", "targetj", "stagelinxj", "japan.stage.xilinx.com", "japan.preview.xilinx.com",
                    "xpcns-target", "xpcns-stagelinx", "cns-target", "cns-stagelinx", "china.stage.xilinx.com", "china.preview.xilinx.com");

prod   = new Array ("www.zylinks.com", "www.origin.zylinks.com",
                    "japan.zylinks.com", "japan.origin.zylinks.com",
                    "china.zylinks.com", "china.origin.zylinks.com",
                    "secure.zylinks.com",
                    "www.xilinx.com", "www.origin.xilinx.com",
                    "japan.xilinx.com", "japan.origin.xilinx.com",
                    "china.xilinx.com", "china.origin.xilinx.com",
                    "secure.xilinx.com");

// Get the hostname.
var hostname = window.location.hostname;


// Determine the language id.
var language = ""; 
if (hostname.indexOf("japan")!= -1 || hostname.indexOf("targetj")!=-1 ||  hostname.indexOf("stagelinxj")!=-1) {
    language = "languageID=2"; 
} else if (hostname.indexOf("china")!= -1 || hostname.indexOf("cns-target")!=-1 ||  hostname.indexOf("cns-stagelinx")!=-1) {
    language = "languageID=3"; 
} 


// Start build the dst_url.
var dst_hostname = "secure.xilinx.com";


// Loop thru INT & DEV envs.
for (i=0; i<devInt.length; i++) {
    if(hostname == devInt[i] || hostname == devInt[i]+".xilinx.com" || hostname.indexOf(".xi.xilinx.com")!= -1) {
        dst_hostname = "secure.xi.xilinx.com";
        break;
    }
}


// Loop thru UAT & PUB envs.
for (i=0; i<uatPub.length; i++) {
    if(hostname == uatPub[i] || hostname == uatPub[i]+".xilinx.com" || hostname.indexOf(".xt.xilinx.com")!= -1) {
        dst_hostname = "secure.xt.xilinx.com";
        break;

    }
}


// Loop thru envs at ISP(ATT) : PROD-TEST & PROD. 
for (i=0; i<prod.length; i++) {
    if(hostname == prod[i] || hostname == prod[i]+".xilinx.com") {
        dst_hostname = "secure.xilinx.com";
        break;
    }
}


var dst_myAcct = "https://" + dst_hostname + "/webreg/register.do?group=myprofile&" + language;
var dst_createAcct = "https://" + dst_hostname + "/webreg/createUser.do?" + language;
var dst_pwdReset = "https://" + dst_hostname + "/webreg/resetPasswordRequest.do?" + language;
var dst_login = "https://" + dst_hostname + "/webreg/loginAction.do?" + language;
var dst_url = "https://" + dst_hostname + "/webreg/register.do?group=myprofile&" + language;

// This function returns the "username" if the user is logged in. 
// Else returns an empty string. 
function getUsername()
{
    var cookies = document.cookie;
    var name = "XilinxUser";
    if (cookies.indexOf("iPlanetDirectoryPro") != -1 &&
        cookies.indexOf("XilinxUser")          != -1) {
        var startpos = cookies.indexOf(name)+name.length+1;
        var endpos = cookies.indexOf(";",startpos);
        if (endpos == -2) endpos = cookies.length;
        return unescape(cookies.substring(startpos,endpos));
    } else {
        return ""; // The user is not logged in.

    }
}


// placed the bgiframe.js code directly inside csi_HF.js so we don't need to add the <link> reference to each page
// bgiframe solves the IE6 Z-index issue with SELECT and hover 

(function($){
$.fn.bgIframe = $.fn.bgiframe = function(s) {
	// This is only for IE6
	if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
		s = $.extend({
			top     : 'auto', // auto == .currentStyle.borderTopWidth
			left    : 'auto', // auto == .currentStyle.borderLeftWidth
			width   : 'auto', // auto == offsetWidth
			height  : 'auto', // auto == offsetHeight
			opacity : true,
			src     : 'javascript:false;'
		}, s || {});
		var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
		    html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
		return this.each(function() {
			if ( $('> iframe.bgiframe', this).length == 0 )
				this.insertBefore( document.createElement(html), this.firstChild );
		});
	}
	return this;
};

})(jQuery);// JavaScript Document
