﻿    var newsIndex = 0;
    var mediaIndex = 0;
    var mediaInterval;
    var stopRotation = 0;
    $(function(){
        newsRotate();
        setInterval(newsRotate,6000);
        setPaging(0);
        //$("#spnRT").html(mediaJson[0].Caption);
        loadMainRotator(0); 
        mediaInterval = setInterval(mediaRotator,6000);
        $("#divPaging").hover(function(){
            clearInterval(mediaInterval);}
        );
    });
    function newsRotate(){
//    try{
        if(newsJson[newsIndex]){
            $("#spNewsHead").html("<a href='"+newsJson[newsIndex].NewsLink+"'>"+newsJson[newsIndex].NewsHead+"</a>");
            newsIndex = (newsIndex +1) % (newsJson.length);
        }
//        }
//        catch{}
    }
    function mediaRotator(){
        
        
        //generate dynamic images
        //slideSwitch();
        //$(function() {    setInterval( "slideSwitch()", 5000 );});
        //mediaIndex = (mediaIndex +1) %(mediaJson.length);
//        $("#spnRT").html(mediaJson[mediaIndex].Caption);
       //loadMainRotator(mediaIndex);
    }
    function loadMainRotator(index){
        if(mediaJson[index]){

            //if(mediaJson[index].Link)
              //  $("#divLink").html('<a href="' + mediaJson[index].Link + '"><img src="images\\spacer.gif" border="0" style="position:absolute; top:0px; left:0px; width:960px; height:400px; cursor: pointer;" /></a>');
            var mediatype = mediaJson[index].MediaType;
            if (mediatype == ".flv" || mediatype == ".swf") {
	            $("#imgMCR").hide();
	            $("#oFlashPlayer").fadeOut(600);
	            $("#spnRT").fadeOut(600);
	             setTimeout(function(){
	                var fpStr = '<embed src="images/mediaplayer.swf" class="ImageL" allowscriptaccess="always" allowfullscreen="true" flashvars="file=' + mediaJson[index].ImageUrl + '&bufferlength=1&showstop=true&autostart=true"/>';
	                $("#oFlashPlayer").html(fpStr);
	                
	                if(mediaJson[index].Caption)
                        $("#spnRT").html(mediaJson[index].Caption);
                     $("#imgMCR").fadeIn(600);   
	                $("#oFlashPlayer").fadeIn(600);
	                setPaging(index);
	            },1000);
	        }
	        else {
	            //$("#imgMCR").fadeOut(600);
//	            $("#spnRT").fadeOut(6000);
//	            setTimeout(function(){
//	                //$("#imgMCR").attr("src",mediaJson[index].ImageUrl);
//	                if(mediaJson[index].Caption)
//                        $("#spnRT").html(mediaJson[index].Caption);
//	                //$("#imgMCR").fadeIn(600);
//	                $("#spnRT").fadeIn(600);
//	                setPaging(index);
//	            },1000);
	            
	            $("#oFlashPlayer").hide("slow");
	        }
        }
    }
    function setPaging(index){
        var pageHtml="";
        for(var j = 0;j<mediaJson.length;j++){
                var imageName = "";
                if(index == j)
                    imageName = "mEna.png";
                else
                    imageName = "mDis.png";   
                 pageHtml += "<img onMouseover='slideSwitchHover("+j+"); clearInterval(mediaInterval);' onclick='loadMainRotator("+j+")'  src='../images/"+imageName+"' alt=''>";
            }
        $("#divPaging").html(pageHtml);
    }
    
    $(function() {    setInterval( "slideSwitch()", 5000 );});
    
    function slideSwitch() 
    {   
        if(stopRotation == 1) return;
        mediaIndex = (mediaIndex +1) %(mediaJson.length);
       
        var $active = $('#slideshow DIV.active');    
        if ( $active.length == 0 ) $active = $('#slideshow DIV:last');    
        var $next =  $active.next().length ? $active.next(): $('#slideshow DIV:first');    
        //$active.addClass('last-active');$("#spnRT").fadeOut(6000);    
        $next.css({opacity: 0.0})
             .removeClass('inactive')
             .addClass('active')
             .animate({opacity: 1.0}, 1000, function() {
             $active.removeClass('active last-active');//$("#spnRT").html(mediaJson[mediaIndex].Caption);$("#spnRT").fadeIn(0); 
             $active.addClass('inactive')            
             setPaging(mediaIndex);
            });
       
     
    }
    function slideSwitchHover(index) 
    {  
        stopRotation = 1;
        mediaIndex = index;
        for(var i=0; i<mediaJson.length; i++)
        {
            if(i==index)
            {
                document.getElementById("div_" + i).className = 'active';                
            }
            else
            {
                if(document.getElementById("div_" + i)) document.getElementById("div_" + i).className = 'inactive';
            }
        }
        setPaging(mediaIndex);
     
    }
    
    function navigateTo(url)
    {
        window.location.href = url;
    }