/**
 * @author Stan Carrico, WHITTMANHART Interactive
 * July, 2009
 * changelog :: 07/28 - updated swf embed height to 630px //SC
 * changelog :: 8/5/09 - updated search validation //SC
 * changelog :: 11/11/09 - WC version, extensive updates to link tracking, added non-flash page view tracking //SC
 * changelog :: 12/10/09 - FluidHomepage //KM
 * changelog :: 12/17/09 - cleanup, revert to standard flash for WC fixes, see comment within embedCallback
 */ 

/* get context */
var host = window.location.host;
var environment = "www";
if(host == "wip.wcm.harley-davidson.com")
	environment = "wip.wcm";
else if(host == "stage.wcm.harley-davidson.com")
	environment = "stage.wcm";
/* end get context */

//GLOBAL VARS
var linkArray = [];
var domLinks = [];

var eSpotLinksA = [];
var eSpotIndexA = [];

var eSpotLinksB = [];
var eSpotIndexB = [];

var docBody;
var isFlashActive = false;

// YUI shorthand
var YUC = YAHOO.util.Connect;
var YUD = YAHOO.util.Dom; 
var YUE = YAHOO.util.Event;
var YUS = YAHOO.util.StyleSheet;



if (typeof home == "undefined" || !home) {
	var home = {};
}

home = {
	
	initPage : function (){
		
		// fix ie non-flash link replacement problem
		if (document.all && swfobject.getFlashPlayerVersion().major == 0) {
            document.getElementById("flashwrapper").style.display = "none";
    		document.getElementById("home_source").style.display = "block";
			setTimeout(function()
			{
				if(!document.getElementById("eSpot_PromoSlot3")) return false;
				document.getElementById("eSpot_PromoSlot3").getElementsByTagName("a")[0].href = "https://profile.harley-davidson.com/cpapp/profile/emailSignupPage.h-d?req_uri=http://www.harley-davidson.com/wcm/Content/Pages/home.jsp&ret_uri=http://www.harley-davidson.com/wcm/il/init_session.h-d&locale=en_US";	
				document.getElementById("eSpot_PromoSlot3").getElementsByTagName("a")[1].href = "https://profile.harley-davidson.com/cpapp/profile/emailSignupPage.h-d?req_uri=http://www.harley-davidson.com/wcm/Content/Pages/home.jsp&ret_uri=http://www.harley-davidson.com/wcm/il/init_session.h-d&locale=en_US";
			}, 500)			
			return false;
		} 

		//YUS('homeTemp').set("#" + 'home_source', {
		//    display : "none"
		//});

        //### Link Tracking Data collection
            var domEls = ["quickstartSection", "whatsNewSection", "eventsSection", "communitySection", "bikesSection", "toolsSection"];
            var eSpotEls = ["TopSectionMain", "BottomPromosMain"];
			var eSpotDivs =[];
            var dIndex = 0;
            
			//loop through regions other than eSpot regions
            for (var d = 0; d < domEls.length; d++) {
                //append new anchor elements to domLinks array using the current domEls region as the root
				if (domLinks.length == 0) {
					domLinks = YUD.getElementsBy(function(el){
						return true;
					}, 'a', domEls[d]);
				}
				else{
					domLinks = domLinks.concat(YUD.getElementsBy(function(el){
						return true;
					}, 'a', domEls[d]));
				}
				//loop through domLinks array starting at the last stored index offset
                for (var i = dIndex; i < domLinks.length; i++) {
                    linkArray[i] = {
                        name: domLinks[i].name,
                        href: domLinks[i].href,
                        onclick: domLinks[i].onclick,
                        target: domLinks[i].target,
						section: domEls[d],
						index : "0"
                    };
                    domLinks[i].href = "event:" + i;
					domLinks[i].onclick = "";					
                }
                dIndex = domLinks.length;
            }
            
            //loop through eSpot regions			
            for (var r = 0; r < eSpotEls.length; r++) {
            
                //store first regions individual eSpot containers
                eSpotDivs[r] = YUD.getElementsByClassName('eSpotIndex', 'div', eSpotEls[r]);

                //loop through individual eSpots
                for (var e = 0; e < eSpotDivs[r].length; e++) {
                
                    //append new anchor elements to domLinks array using the current eSpot as the root
                    if (domLinks.length == 0) {
                        domLinks = YUD.getElementsBy(function(el){
                            return true;
                        }, 'a', eSpotDivs[r][e]);
                    }
                    else {
                        domLinks = domLinks.concat(YUD.getElementsBy(function(el){
                            return true;
                        }, 'a', eSpotDivs[r][e]));
                    }
                    
                    //loop through domLinks array starting at the last stored index offset
                    for (var i2 = dIndex; i2 < domLinks.length; i2++) {
                        linkArray[i2] = {
                            name: domLinks[i2].name,
                            href: domLinks[i2].href,
                            onclick: domLinks[i2].onclick,
                            target: domLinks[i2].target,
                            index: e,
							section: eSpotEls[r]
                        };
                        domLinks[i2].href = "event:" + i2;
						domLinks[i2].onclick = "";
                    }
                    //store the new array length as the index offset for the next loop
                    dIndex = domLinks.length;
                }
            }
		//### END Link Tracking Data collection
		home.initFlash();
	},
	/**
	 * @method initFlash initializes the flash embed/detect
	 */
	initFlash : function (){ // not used, now FluidHomepage
		
		flashvars = {};
		params = {
			wmode: "opaque"
		};
		attributes = {
			id: "home_flash_app",
			name: "home_flash_app"
		};
		
		//swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes, callbackFn)
		swfobject.embedSWF('/en_US/Media/flash/Apps/US_Homepage/HD_Homepage.swf', 'home_flash', '980', '630', '9.0.115.0', '',flashvars,params,attributes,home.embedCallback);	
		
	},
	/**
	 * @method embedCallback handles flash embed success / failure states
	 * @param {Object} response
	 * 
	 */
    embedCallback: function(response){
        isFlashActive = response.success;
		
		
        if (response.success != true) {
			if(typeof isNonFlashTracked == "undefined")
			{
				_hbPageView("non+flash", "/Home/");
				isNonFlashTracked = true;				
			}
    		document.getElementById("flashwrapper").style.display = "none";
    		document.getElementById("home_source").style.display = "block";
			
            YUS('homeTemp').disable();
            for (var i = 0; i < domLinks.length; i++) {
                domLinks[i].href = encodeURI("javascript:void(0)");
                domLinks[i].target = "";
                domLinks[i].name = i;
                domLinks[i].onclick = function(){
                    home.app.getLink(this.name, 'Javascript', '', '');
                };
            }
        }
    }
};
