// JavaScript Document

/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

if ( location.protocol.substr( 0, 4 ) == "file" ) {

    document.write(
        'This example uses the XmlHttpRequest object and cannot be run locally.'
      + 'You must copy it to a web server and access it using HTTP.'
    );

} else {

    // The initial section will be chosen in the following order:
    //
    // URL fragment identifier (it will be there if the user previously
    // bookmarked the application in a specific state)
    //
    //         or
    // "section" URL parameter (it will be there if the user accessed
    // the site from a search engine result, or did not have scripting
    // enabled when the application was bookmarked in a specific state)
    //
    //         or
    //
    // "home" (default)

    var bookmarkedSection = YAHOO.util.History.getBookmarkedState( "app" );
    var querySection = YAHOO.util.History.getQueryStringParameter( "section" );
    var initSection = bookmarkedSection || querySection || "home";

    // Register our only module. Module registration MUST take
    // place before calling YAHOO.util.History.initialize.
    YAHOO.util.History.register( "app", initSection, function( state ) {
        // This is called after calling YAHOO.util.History.navigate, or after the user
        // has trigerred the back/forward button. We cannot discrminate between
        // these two situations.
        loadSection( state );
    } );

    // This function does an XHR call to load and
    // display the specified section in the page.
    //Yahoo GET Ajax function
	function loadSection( url ) {
			
		var containerid = "divactualcontent";
		
	   // var url = "./inc/" + section + ".php";
	   var breakdownURL = url.split("_");
	   var realURL = "specialproducts/"+breakdownURL[0]+".php?title="+breakdownURL[1];
		function successHandler( obj ) {
			// Use the response...
			// THE REQUEST WAS SUCCESSFUL - DISPLAY THE PRODUCT CONTENT
			//YAHOO.util.Dom.get( "bd" ).innerHTML = obj.responseText;
			
			document.getElementById(containerid).innerHTML=obj.responseText;
			showhide('divproductpopupzone', 'show');

			
	
		}
	
		function failureHandler( obj ) {
			// Fallback...
			//location.href = "?section=" + section;
			alert("failed to load");
		}
		if (url != "home") { //if the content is just the catalog not a product don't load anything because it's already there
			//show the loading cover
				if (document.all) { //IS IE 4 or 5 (or 6 beta)
					eval( "document.all." + 'divloadingmarker' + ".style.visibility = 'visible'");
				}
				if (document.layers) { //IS NETSCAPE 4 or below
					document.layers['divloadingmarker'].visibility = 'visible';
				}
				if (document.getElementById && !document.all) {
					maxwell_smart2 = document.getElementById('divloadingmarker');
					maxwell_smart2.style.visibility = 'visible';
				}
			YAHOO.util.Connect.asyncRequest( "GET", realURL,
				{
					success:successHandler,
					failure:failureHandler
				}
			);
		} else {
			//this is the home catalog (no product pop up) so hide it
			if (document.all) { //IS IE 4 or 5 (or 6 beta)
				eval( "document.all." + 'divproductpopupzone' + ".style.visibility = hidden");
				eval( "document.all." + 'divloadingmarker' + ".style.visibility = hidden");
			}
			if (document.layers) { //IS NETSCAPE 4 or below
				document.layers['divproductpopupzone'].visibility = "hidden";
				document.layers['divloadingmarker'].visibility = "hidden";
			}
			if (document.getElementById && !document.all) {
				maxwell_smart = document.getElementById('divproductpopupzone');
				maxwell_smart.style.visibility = "hidden";
				maxwell_smart2 = document.getElementById('divloadingmarker');
				maxwell_smart2.style.visibility = "hidden";
			}
		}
	}

    function initializeNavigationBar() {
        // Process links
        var anchors = YAHOO.util.Dom.get( "specialcatalog" ).getElementsByTagName( "a" );
        for ( var i=0, len=anchors.length ; i<len ; i++ ) {
            var anchor = anchors[i];
            YAHOO.util.Event.addListener( anchor, "click", function( evt ) {
                var href = this.getAttribute( "href" );
                var section = YAHOO.util.History.getQueryStringParameter( "section", href ) || "specialprod.php";
                // If the Browser History Manager was not successfuly initialized,
                // the following call to YAHOO.util.History.navigate will throw an
                // exception. We need to catch it and update the UI. The only
                // problem is that this new state will not be added to the browser
                // history.
                //
                // Another solution is to make sure this is an A-grade browser.
                // In that case, under normal circumstances, no exception should
                // be thrown here.
                try {
                    YAHOO.util.History.navigate( "app", section );
                } catch ( e ) {
                    loadSection( section );
                }
                YAHOO.util.Event.preventDefault( evt );
            } );
        }

        // This is the tricky part... The window's onload handler is called when the
        // user comes back to your page using the back button. In this case, the
        // actual section that needs to be loaded corresponds to the last section
        // visited before leaving the page, and not the initial section. This can
        // be retrieved using getCurrentState:
        var currentSection = YAHOO.util.History.getCurrentState( "app" );
        if ( location.hash.substr(1).length > 0 ) {
            // If the section requested in the URL fragment is different from
            // the section loaded in index.php, we have an unpleasant refresh
            // effect because we do an asynchronous XHR call. Instead of doing
            // a synchronous XHR call, we can fix this by erasing the initial
            // content of bd:
			
            if ( currentSection != querySection )
                YAHOO.util.Dom.get( "divactualcontent" ).innerHTML = "";
            loadSection( currentSection );
			
        }
    }

    // Subscribe to this event before calling YAHOO.util.History.initialize,
    // or it may never get fired! Note that this is guaranteed to be fired
    // after the window's onload event.
    YAHOO.util.History.onLoadEvent.subscribe( function() {
        initializeNavigationBar();
    } );

    // The call to YAHOO.util.History.initialize should ALWAYS be from within
    // a script block located RIGHT AFTER the opening body tag (this seems to prevent
    // an edge case bug on IE - IE seems to sometimes forget the history when
    // coming back to a page, and the back - or forward button depending on the
    // situation - is disabled...)
    try {
        YAHOO.util.History.initialize( "blank.html" );
    } catch ( e ) {
        // The only exception that gets thrown here is when the browser is not A-grade.
        // Since scripting is enabled, we still try to provide the user with a better
        // experience using AJAX. The only caveat is that the browser history will not work.
        initializeNavigationBar();
    }
}
//------------------------------------------------- DEREK CODE ------------------------------------------------------------------
//----------------- SHOW HIDE DIV ----------------------
var state = 'hidden';

function showhide(layer_ref, visibility) {
	state = visibility;
	if (state == 'hide') {
		state = 'hidden';
		top.location.href = "http://glowproducts.com/specials/#app=home";
		//YAHOO.util.History.navigate( "app", 'home' );
	}
	else {
	state = 'visible';
	}
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + layer_ref + ".style.visibility = state");
		eval( "document.all." + 'divloadingmarker' + ".style.visibility = state");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[layer_ref].visibility = state;
		document.layers['divloadingmarker'].visibility = state;
	}
	if (document.getElementById && !document.all) {
		maxwell_smart = document.getElementById(layer_ref);
		maxwell_smart.style.visibility = state;
		maxwell_smart2 = document.getElementById('divloadingmarker');
		maxwell_smart2.style.visibility = state;
	}

}
