﻿/*
functions required by Jeroplayer to communicate with flash.
*/

var currentItem = 0;
var totalItemsCurrentList = 0;
var startVideo;

// these functions are caught by the JavascriptView object of the player.
function sendEvent(typ,prm) { 

    // sends event to to the SWF
	thisMovie("mpl").sendEvent(typ,prm); 

};

// called from the SWF player	!
function getUpdate(typ,pr1,pr2,pid) {

    // done?
	if (typ == "state") {
	
	    if (pr1 == 3){
    		nextStep();
    	}
    }    	

    // state of the player (3 = clip complete)
    if (typ == "volume") { 
	    // SWF has passed back the current volume
		currentVolume = pr1; 
	} 
	else if (typ == "time") 
	{ 

	    // "pr1" is passed back from the flash object
	    // in this case it means the current position in the clip
        currentPosition = pr1; 

	}
	else if(typ == "item") { 
		currentItem = pr1; 
	}		
};

function loadFile(obj) {

    thisMovie("mpl").loadFile(obj); 
    
};

function addItem(obj,idx) { thisMovie("mpl").addItem(obj,idx); }

function removeItem(idx) { thisMovie("mpl").removeItem(idx); }

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};

var currentTab = new Object();

function CountCurrentAndHideInactive(catToShow) {

    var totalThisCat = 0;

	$('#playList>div').each(
	
		function( intIndex ) {
		
		    var rowCat = 
                $(this).attr('class').replace("cat_", "");
                
		    if (rowCat == catToShow) {
		    
		        totalThisCat++;
		    
		        $(this).show();		    
		    
		    } else {
		    
		        $(this).hide();		    
		    
		    }
				
		}
	);

    $('#playList').jScrollPane({animateTo: false});  

	return totalThisCat;

}

// JScript File
function initTabs() {

	$('div.topTab_off').each(
	
		function( intIndex ) {
		
            $(this).click(function() {
	            flipTab(this);
            });		
		
			if (intIndex == startCat){
			
			    currentTab = this;
				$(this).removeClass('topTab_off');
				$(this).addClass('topTab_on');				
				
			} 
		}
	);

    $('span.header1').click(function() {
    
		doIggy(this);
		
    });
    
}


function flipTab(tabToFlip) {

    startVideo = "null";
    sendEvent('stop');
    
    var newID = 
        $(tabToFlip).attr("id").replace(/tabTop_/, "");

    if ($(tabToFlip).attr("id") == "tabTop_6") {
    
        var thisURL = "/Videos/PlayList_Audio.aspx%3Fi%3D" + 
            newID + "%26s%3Dnull";    
    
    } else {        
    
        var thisURL = "/Videos/PlayList.aspx%3Fi%3D" + 
            newID + "%26s%3Dnull";

		//asdf = prompt("-", thisURL);
            
    }            

	//alert(newID);
    
    var newOBJ = new Object();    
    newOBJ.file = thisURL;
    loadFile(newOBJ);
    
    //sendEvent('playitem', 0);        
    
    CountCurrentAndHideInactive(newID);
    
    changeTabStatus(tabToFlip);

    $("#playList")[0].scrollTo(0, {animateTo: false});

    currentTab = tabToFlip;
    
}

function nextStep() {

    sendEvent('stop');    
    
   /* if (currentItem > totalItemsCurrentList)
    {

        sendEvent('stop');
    }
    else
    {
        sendEvent('stop');    
        
        if (startVideo != "null") {
            currentItem++
        }        
        
        currentItem++;
        
       
        sendEvent('playitem', (currentItem));
       // sendEvent('playpause');             
    }
    
    */


}

function SwitchItem(thisItem) {

    if (startVideo != "null") {
        thisItem++;
    }

    sendEvent('playitem', thisItem);

}

function createPlayer(playListID, autoStart) {

    var thisURL = "/Videos/PlayList.aspx%3Fi%3D" + 
        playListID + "%26s%3D" + 
        startVideo;

   //var asdf = prompt("-", thisURL);

    var so = new SWFObject('/includes/FlashObjects/mediaplayer2.swf','mpl','350','288','8');
    so.addParam("allowfullscreen","true");
    so.addParam("wmode", "transparent");
    so.addVariable("file", thisURL);
    so.addVariable("enablejs","true");
    so.addVariable("javascriptid","mpl");
    so.addVariable("bufferlength","1");	    
    so.addVariable("autostart", autoStart);
    so.addVariable("showicons", "false");
    so.addVariable("showstop", "true");    
    so.addVariable("repeat","false");
    so.addVariable("volume", "40");
    so.addVariable("shuffle","false");
    so.addVariable("displayheight","268");
    so.addVariable("width","350");
    so.addVariable("height","288");
    so.addVariable("overstretch", "fit");
    so.write("playerHolder");   
       
}

window.onload = initialize;

function initialize() {


    if (startVideo == "null") {
    
        createPlayer(1, false);
        
    } else {

        createPlayer((startCat + 1), false);

        setTimeout(function(){

            sendEvent('playitem', 0);
            currentItem = 0;
        
        }, 1400);        
                
    }
    
    initTabs();
    //var thisTab = $("#tabTop_" + startCat);
    
  //  changeTabStatus(thisTab);
    
    totalItemsCurrentList = CountCurrentAndHideInactive(startCat+1);
    $('#playList').jScrollPane();    
  
}


function changeTabStatus(incomingTab) {

    $(currentTab).removeClass('topTab_on');
    $(currentTab).addClass('topTab_off');
   
    $(incomingTab).removeClass('topTab_off');
    $(incomingTab).addClass('topTab_on');  

}
