var ns6 = document.getElementById && !document.all;
function getUrl(PVID, reelID, referenceID, tid, isDedicated) {
    var distribution = getQueryVariable("dist");
    var newURL = baseDomain + "/fc/LoadFC.do?pvid=" + PVID + "&reel=" + reelID + "&ref=" + referenceID + "&tid=" + tid + "&pn=" + personNumber + "&dist=" + distribution + "&dedicated=" + isDedicated;
    return newURL;
}

function doSlideFromImage() {
    if (showScrollerItems) {
        isNew = scrollingLinksArray[currentScrollIndex].isNewSystem;
        reelId = scrollingLinksArray[currentScrollIndex].reelID;
        pvid = scrollingLinksArray[currentScrollIndex].PVID;

        DisplaySlide( 'slideContent' , reelId, pvid, 785 , 580 , 1 , 2000 , 1, (isNew ? 2 : 1), pviReferenceID, 6)
    } else {
        window.open("http://www.peerviewpress.com");
    }
}


function hideControls() {

    if (ns6) {
        document.getElementById("hideSection1").style.visibility = 'hidden';
        document.getElementById("hideSection2").style.visibility = 'hidden';
    } else {
        document.all['hideSection1'].style.visibility = 'hidden';
        document.all['hideSection2'].style.visibility = 'hidden';
    }
}

function showControls() {

    if (ns6) {
        document.getElementById("hideSection1").style.visibility = 'visible';
        document.getElementById("hideSection2").style.visibility = 'visible';
    } else {
        document.all['hideSection1'].style.visibility = 'visible';
        document.all['hideSection2'].style.visibility = 'visible';
    }
}

function autoServeSlide(pvid, maxReels, displayMethod, referenceId, clickThroughTypeID, isDedicated) {

    if (WM_readCookie( "fcshown" ) == "1") {
        return;
    }

    doSlide = false;
    showReel = 1;
    cookieName = "pvin_" + pvid;
    cookieVal = WM_readCookie(cookieName);

    if (displayMethod == 0){
        doSlide = true;
    }

    if (cookieVal == "" || cookieVal == null || cookieVal.length < 1) {
        doSlide = true;
    } else {
        var currentCounter = eval(cookieVal.charAt(cookieVal.length-1));
        if (currentCounter < maxReels) {
            showReel = currentCounter + 1;
            doSlide = true;
        }
    }

    if (doSlide) {
        handle = setTimeout("DisplaySlide('slideContent', " + showReel + ", " +  pvid + ", 785 , 580  , 1 , 2000 ," + displayMethod + ", 2, " + referenceId + ", " + clickThroughTypeID + ", " + isDedicated + ");",200);
        if (displayMethod == 1){
            WM_setCookie(cookieName , showReel, 24 * 365);
            WM_setCookie ('fcshown', '1', 0.25, '');
        }
        fcshown = true;
        InterstitialSeen = 1;
    }
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Name   : slideFunctionsjs_isACCMEBMSDisplay
//
// Desc   : Do we need to call the accme function in order to display it ?
//
//          Based on the fact that the accme.inc is include and if the bussiness rule
//          apply (see accme_isACCMEBMSDisplay in accme.inc).
//          When var accme_inc_loaded it mean that the accme.inc file is include in the document
//
// return : boolean value 
//          true  = the caller must call the appropriate accme function
//          false = the caller must not call a accme function
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function slideFunctionsjs_isACCMEBMSDisplay(){
    //alert ("slideFunctionsjs_isACCMEBMSDisplay(): typeof accme_inc_loaded="+typeof accme_inc_loaded);
	if (typeof accme_inc_loaded == "undefined" ){
      return false;	
	}  else  {
		return accme_isACCMEBMSDisplay();
	} 
}

function DisplaySlide( name , newReelID, newPVID, iwidth , iheight , autoclose , duration , islide, version, referenceID, ctTypeID, isDedicated )
{
	if (slideFunctionsjs_isACCMEBMSDisplay() == true ){
		accme_display_fromDisplaySlide(  name , newReelID, newPVID, iwidth , iheight , autoclose , duration , islide, version, referenceID, ctTypeID, isDedicated );
	} else {
		slideFunctionsjs_doDisplaySlide( name , newReelID, newPVID, iwidth , iheight , autoclose , duration , islide, version, referenceID, ctTypeID, isDedicated );
	}
}

function closeNewMediaPlayer()
{
	var	iframe;

	iframe=$("#idNewMediaPlayer iframe").get(0);
	if(iframe && iframe.contentWindow.doXCloseModified) {
		iframe.contentWindow.doXCloseModified();
	}
}

function slideFunctionsjs_doDisplaySlide( name , newReelID, newPVID, iwidth , iheight , autoclose , duration , islide, version, referenceID, ctTypeID, isDedicated )
{
	if (slidePanelActive)  {
	    clearInterval(surveyIntervalID);
	    secs = 0;
	} else {
	    slidePanelActive = true;
	}
	if (isDedicated == null){
	    isDedicated = false;
	}
	
	if (version>1) {
		var	elem,dims;

		dims=viewPortDimensions();

		elem=lpu_open("idNewMediaPlayer",
			getUrl(newPVID,newReelID,referenceID,ctTypeID,isDedicated),
			"",
			iwidth+13,
			iheight+15,
			false,false,
			0,0,
			true,
			function() {
				closeNewMediaPlayer();
				},
			false
			);
		elem.hide();

		sl_slide(elem,
			(dims.width-elem.width())/2,
			dims.height,
			(dims.width-elem.width())/2,
			(dims.height>elem.height() ? dims.height-elem.height() : 0)
			);
    } else {
        // This is a version 1 popup.
        var baseURL = "http://ww3.peerviewpress.com/misc/fc.nsf/dispatch/" + scrollingLinksArray[currentScrollIndex].pvName;
        var windowHeight = scrollingLinksArray[currentScrollIndex].windowHeight;
        var url = baseURL + '?OpenDocument&ref=' + fcPageType + '&video=' + scrollingLinksArray[currentScrollIndex].reelID + "&dist=" + distType + "&num=" + distNumber;

        WindowWidth=700;
        leftPosition = (screen.width) ? (screen.width-WindowWidth)/2 : 0;
        topPosition = (screen.height) ? (screen.height-windowHeight)/2 : 0;
        window.open(url,"container","width=700,height=" + windowHeight + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",top=" + topPosition + ",left=" + leftPosition + ",menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no,location=no");

        scrollerClickThroughNumber = -1;
        scrollerRequestedNumber = 1;
    }
}

function CloseReminder( name ) {
    slidePanelActive = false;
    if (ns6) {

        layerFrame =  document.getElementById("slideContent").style ;
        layerFrame.top="0px";

    } else {

        with ( slideBanner = cbeGetElementById( name ).cbe) {
             slideTo(144,-1 * height(), 1000);
        }

    }
}

function CloseSlide( name , islide )
{
	var	version,elem;

	version=1;
	elem=document.getElementById("scrollDiv");
	if (elem && elem.mediaPlayerVersion) {
		version=parseInt(elem.mediaPlayerVersion);
		}

    slidePanelActive = false;
    if (ns6) {
        if (navigator.userAgent.indexOf("Firefox")==-1 && !isSafari) {
            try {
                var flashMovie=frames["slideIFrame"].document.getElementById('inSession');
                flashMovie.StopPlay();
            } catch (e) {
                var flashMovie=frames["slideIFrame"].document.inSession;
                flashMovie.StopPlay();
            }
            flashMovie.TStopPlay("screen");
        }

		if (islide==1 && version==3) {
	        with ( slideBanner = cbeGetElementById( name ).cbe) {
               	var	dims=viewPortDimensions();
                slideTo((dims.width-width())/2,dims.height,1000,hideIFrame);
            	}
			}
		else {
	        layerFrame =  document.getElementById("slideContent").style ;
    	    layerFrame.top="0px";
        	dropstart=setInterval("dropin()",10)
        	}

        //showControls();
        if (window.bouncestart) clearInterval(bouncestart);

        intervalID = setInterval('switchContent();', scrollSpeed);
        scrollIntervalID = setTimeout('showScroll();', 1000);

    } else {
        //showControls();

        clearInterval(surveyIntervalID);

        //showSurvey();

        if (parent.flashVersion >= 5  || parent.flashVersion == null || parent.flashVersion == 0) {

            var flashObj = frames["slideIFrame"].document.all['inSession'];
			if (flashObj) {
	            flashObj.stop();
	            flashObj.TStopPlay("container");
	            flashObj.TStopPlay("screen");
	            }

        }

        with ( slideBanner = cbeGetElementById( name ).cbe) {
            if( islide == 1 ) {
            	if (version<3) {
	                slideTo(0,-1 * height(), 1000);
	                }
	            else {
                	var	dims=viewPortDimensions();
 	                slideTo((dims.width-width())/2,dims.height, 1000,hideIFrame);
	            	}
            } else {
                hide();
            }
        }

        intervalID = setInterval('switchContent();', scrollSpeed);
        scrollIntervalID = setTimeout('showScroll();', 1000);
    }
}

function viewPortDimensions()
{
	var	w,h;

	w=(ns6 ? window.innerWidth : document.documentElement.clientWidth);
	h=(ns6 ? window.innerHeight : document.documentElement.clientHeight);
	return { width: w, height: h };
}

function hideIFrame()
{
	var	elem;

	elem=document.getElementById("slideContent");
	if (elem) {
		elem.style.visibility="hidden";
		elem.style.left="-1000px"; //fix for IE6
		elem.style.top="0px";
		}
}

function dropin(){
    scroll_top= window.pageYOffset
    layerFrame_height = layerFrame.height;
    if (parseInt(layerFrame.top) > -parseInt(layerFrame_height))
        layerFrame.top=parseInt(layerFrame.top)-30+"px"
    else{
        layerFrame.visibility="hidden";
        layerFrame.display="none";
        clearInterval(dropstart);
        controlBannerFlash();
    }
}

function showScroll() {
    if (ns6) {
        document.getElementById("scrollDiv").className="scrollHide";
    }else {
        document.all["scrollDiv"].className="scrollHide";
    }
}


// Verify is threre are  flash banner and hide or show them (for mozilla and firefox)
function controlBannerFlash() {
    var flashID;
    var nbMaxBannerFlash = 10;
    for (var i = 1; i <= nbMaxBannerFlash; i++) {
        flashID =  "FlashMovie" + i;
        if(document.getElementById(flashID) != null) {
            bannerFlash = document.getElementById(flashID).style ;
            if (bannerFlash.visibility == "visible" || bannerFlash.visibility == "") {
                bannerFlash.visibility="hidden";
            } else {
                bannerFlash.visibility="visible";
            }
        } else {
            break;
        }
    }
}

function getWindowWidth() {
    if (is.nav4) {
        return window.innerWidth;
    } else {
        return document.body.clientWidth;
    }
}

function getWindowHeight() {
    if (is.nav4) {
        return window.innerHeight;
    } else {
        return document.body.clientHeight;
    }
}

function getCenterLeft( iwidth ) {
    var x;
    if (getWindowWidth() < iwidth) {
        x=0;
    } else {
        var windowWidth = getWindowWidth();
        x = Math.round((windowWidth/2) - (iwidth/2));
    }
    return x;
}

function getCenterTop( iheight ) {
    var y;

    if (getWindowHeight() < iheight) {
        y=0;
    } else {
        y = Math.round((getWindowHeight()/2) - (iheight/2));
    }
    return y;
}
