var currentlyover = 0;
var PopupsReset = 0;

var minX=0;
var maxX=500;
var minY=0;
var maxY=500; 
var x,y;
var flag = false;
var SliderId = '';
var SliderMax = '';
var PositionOverride = false;
var CurrentPosition = -1;
var CurrentPopup = '';
var CurrentUserPopup1 = '';
var CurrentPInstructors = '';
var CurrentImage = '';
var FirstTimeVisit = false;


function View(obj) {
    document.getElementById(obj).style.display = 'block';
    var divInstructor = $get('divInstructors');
    divInstructor.style.display = 'none';
}

function Close(obj) {
    document.getElementById(obj).style.display = 'none';
}

function ResetPopups() {
    var popups = jQuery(" .popup");
    for(var l=0, ll=popups.length; l<ll; l+=1) {
        popups[l].style.visibility = 'Hidden';
        //popups[l].style.zIndex = '600';
    }
}

function ShowPopup(hoveritem,div,fromdiv,extradiv,userdiv,hide)
{ 
    if(PopupsReset != 0) {
        ResetPopups();
    }
    //alert('works');
    PopupsReset = 1;
    var slider = $get(SliderId);
    if(slider == null) {
        return;
    }
    
    //jQuery(hoveritem).css('zIndex', 8000); 
    
    var value = slider.value;
    var hp = document.getElementById(div);
    var hp2 = document.getElementById(userdiv);
    if(fromdiv == '0') {
        var top, left;
        var obj = hoveritem;
        var curTop=0,curLeft=0;
        if(obj.offsetParent) {
        do {
            curLeft += obj.offsetLeft;
            curTop += obj.offsetTop;
            } while(obj = obj.offsetParent)
        }
        var offset = jQuery(hoveritem).css('offset');

        //get x/y value for popup
        top = curTop - minY - 150; 
        left = curLeft - minX; 

        var toptemp, lefttemp;
        toptemp = top;
        lefttemp = left;

        var upperbound;
        upperbound = parseInt(value,10)+parseInt(maxX,10)-parseInt(hp.clientWidth,10);
        //get x value for draggable popup
        if(upperbound<=lefttemp){lefttemp=upperbound;}
        else if(lefttemp<value){lefttemp=value;}
        else lefttemp = lefttemp; //length of a popup
        //get y value for draggable popup
        if(toptemp>=maxY){toptemp=maxY;}
        else if(toptemp<minY){toptemp=minY;}
        else toptemp = toptemp;

        hp.style.top = toptemp + 'px';
        hp.style.left = lefttemp + 'px';

        var xLocation, yLocation;
        xLocation = left-value+minX+parseInt(hp.clientWidth,10);
        yLocation = top + minY + 27;
        
        //get x value for draggable popup
        if(xLocation>=maxX){flag=true; x=maxX;}
        else if(xLocation<minX){flag=true; x=minX;}
        else x = xLocation; //length of a popup
        //get y value for draggable popup
        if(yLocation>=maxY){flag=true; y=maxY;}
        else if(yLocation < minY){flag=true; y=minY;}
        else y = yLocation;
        
        var finalLocation = new Sys.UI.Point(x,y);
        $find('bInstructors').set_location(finalLocation);
        
        document.getElementById(extradiv).style.display = 'none';
        currentlyover = 0;
    } else {
        currentlyover = 1;
    }
    
    if(hide == '1') {
        hp2.style.visibility = 'hidden';
    }
    hp.style.visibility = 'Visible';
}

function HidePopup(div,fromdiv,userdiv,hide) 
{        
    if(fromdiv == '0') {
        currentlyover = 0;
        setTimeout("HidePopup('" + div + "','1','" + userdiv + "','" + hide + "')", 2000);
    } else {
        if(currentlyover == 0) {
            var hp = document.getElementById(div);
            hp.style.visibility = 'Hidden';
            var hp2 = document.getElementById(userdiv);
            if(hide == '1') {
                hp2.style.visibility = 'Visible';
            }
        }  
    }
}

function Scroll(obj)
{
    if(PopupsReset != 0) {
        ResetPopups();
    }
    PopupsReset = 1;
    var slider = $get(SliderId);
    var value = slider.value;
    document.getElementById(obj).scrollLeft = value;
}

function runSIFROE(){
    if(typeof sIFR == "function"){
        sIFR.replaceElement("h1.name", named({sFlashSrc: '/style/les-mills.swf', sColor: "#c4c4c4", sCase: "upper", sWmode:"transparent" }));
        sIFR.replaceElement("h3.bottom", named({sFlashSrc: '/style/les-mills-tone.swf', sColor: "#ffffff", sWmode:"transparent", sFlashVars: "textalign=center" }));
        sIFR.replaceElement("h2.year", named({sFlashSrc: '/style/les-mills.swf', sColor: "#ffffff", sCase: "upper", sWmode:"transparent", sFlashVars: "textalign=center" }));
        sIFR.setup();
    };
}

var myWidth = 0, myHeight = 0;
function Size() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  myHeight = ((myHeight / 2) - 318);
  myWidth = ((myWidth / 2) - 450);
  document.getElementById('idtop').style.paddingTop = myHeight + 'px';  
  
  minX=parseInt(myWidth, 10);
  maxX=900;
  minY=parseInt(myHeight, 10);
  //deduct height of a draggable div (but in points not pixels)
  maxY=636 - 200; 
}

function pageLoad(){
    $find('bInstructors').add_move(onMove);
}

function onMove(){
   flag = false;
   var el = $find('bInstructors').get_element();
   var newLocation = $common.getLocation(el);
   //get x value
   if(newLocation.x>=maxX){flag=true; x=maxX;}
   else if(newLocation.x < minX){flag=true; x=minX;}
   else x = newLocation.x;
   //get y value
   if(newLocation.y>=maxY){flag=true; y=maxY;}
   else if(newLocation.y < minY){flag=true; y=minY;}
   else y = newLocation.y;
   //reset to the new position
   if(flag){
       var finalLocation = new Sys.UI.Point(x,y);
       $find('bInstructors').set_location(finalLocation);
   }          
}

Sys.Application.add_init(AppInit);

function AppInit() {
  Sys.Application.add_load(RunOnce);
}

function RunOnce() {
    var object = document.getElementById('ctl00_Content_ctl00_ctl00_temp');
    if(PositionOverride == false) {
        object.value = SliderMax;
    } else {
        object.value = CurrentPosition;
    }
    if (document.all)
    {
        object.fireEvent("onchange");
    }
    else
    {
        var changeEvent = document.createEvent("Event");
        changeEvent.initEvent("change", false, true);
        object.dispatchEvent(changeEvent);
    } 
    Scroll('ctl00_Content_ctl00_ctl00_innerdiv');
    if(PositionOverride == true) {
        var ImageObject = document.getElementById(CurrentImage);
        ShowPopup(ImageObject,CurrentPopup,'0',CurrentPInstructors,CurrentUserPopup1,'1');
    }
    PositionOverride = false;
    FirstTime();
    Sys.Application.remove_load(RunOnce);
}


function EndRequestHandler(sender, args){
    var divInstructor = $get('divInstructors');
    divInstructor.scrollTop = 0
    divInstructor.style.display = 'block';
}

function SetPositionIfReturning(PACurrentPosition,PACurrentPopup,PACurrentUserPopup1,PACurrentPInstructors,PACurrentImgGroupImage) {
    CurrentPosition = PACurrentPosition;
    CurrentPopup = PACurrentPopup;
    CurrentImage = PACurrentImgGroupImage;
    CurrentPInstructors = PACurrentPInstructors;
    CurrentUserPopup1 = PACurrentUserPopup1;
    PositionOverride = true;
}

function FirstTime() {
    if(FirstTimeVisit == true) {
        ShowModalPopup();
    }
}

function ShowModalPopup() { 
    var modalpopup = $find('programmaticModalPopupBehavior'); 
    modalpopup.show(); 
}