﻿// JScript File

// Code to make the cufflink functionality work
// Outgoing:    create session cookie with url of current page              [CANNOT READ COOKIES FROM OTHER DOMAINS - REWRITE]
//              check for existing cookie from called site and use url
//              append query string '?CuffLink' to url call
//
// Incoming:    check for cufflink query string
//              slide pane if present otherwise just reveal it

var mySitecookie = 'SmokeFreeCuffLinkcookie';
//var gotoSitecookie = 'StopSmokingCuffLinkcookie';
var CuffLinkGotoURL = $('dnn_CuffLink').href;

var mySliderElement = 'cufflinkSlider';
var effectDuration = 1000; 

    
function checkforCuffLink(){
    //check to see if calling url has the cufflink query string
    var n= document.location.href.indexOf('?CuffLink');
    if (n > -1) {
        reappearRight();
    } else {
        reveal();
    }
}
    
// the whole site contained by the slider is hidden using a CSS class on load
// to prevent it from flashing up briefly before the page can slide in.
// if javascript is disabled, the hidden class is overridden by embedded
// style in the <noscript> block
function reveal(){
    // remove the 'hidden' class from the slider
    $(mySliderElement).className = $(mySliderElement).className.replace('hidden','');
}    

function disappearRight(){
    $(mySliderElement).className = $(mySliderElement).className += ' start';
	var myEffects = new Fx.Styles(mySliderElement, {unit:'%', duration: effectDuration, transition: Fx.Transitions.linear});
	myEffects.custom({
		'margin-left':[0, 120]
	});
    var cookiehref = String(document.location)
    var n= cookiehref.indexOf('?CuffLink');
    if (n > -1){
        // slice the cufflink query string off the end
        cookiehref = cookiehref.slice(0,n);
    }  
	createCookie(mySitecookie, cookiehref, 0);
//	var storedhref = readCookie(gotoSitecookie);
//	if (!storedhref) {
//	    // if no stored cookie the use the default home page
//	    storedhref = CuffLinkGotoURL;
//	    }
//    window.setTimeout("document.location = '" + storedhref + "?CuffLink';", effectDuration+100);
    window.setTimeout("document.location = '" + CuffLinkGotoURL + "?CuffLink=True';", effectDuration+100);
}

function reappearRight(){
    //check to see if calling url has the cufflinkstored query string
    var n= document.location.href.indexOf('?CuffLinkStored');
    if (n > -1) {
    } else {
	    var storedhref = readCookie(mySitecookie);
	    if (storedhref) {
	        // if stored cookie then reload stored page
	        document.location = storedhref+ '?CuffLinkStored=True';
            return;
	    }
    }
	    
    // add the class 'start' to set width and height to 100%
    $(mySliderElement).className = $(mySliderElement).className += ' start';
    // add the class 'movetoright' to set margin-left to 100%
    $(mySliderElement).className = $(mySliderElement).className += ' movetoright';
    // wait a bit and then unhide the slider
    window.setTimeout('reveal();',20);
	var myEffects = new Fx.Styles(mySliderElement, {unit:'%', duration: effectDuration, transition: Fx.Transitions.linear});
	myEffects.custom({
		'margin-left':[120, 0]
	});
	// remove the added classes
    $(mySliderElement).className = $(mySliderElement).className.replace(' start','');
    $(mySliderElement).className = $(mySliderElement).className.replace(' movetoright','');

}

//window.addEvent('load', function() {
window.addEvent('domready', function() {
    
    checkforCuffLink();
    
    // remove the default url, which allows the site to work without javascript
    // add the click event to slide the page and make the cufflink call
    $('dnn_CuffLink').href = "#";
    $('dnn_CuffLink').addEvent('click', function(){disappearRight()});
    
},'','javascript');



