﻿var newsIndex = 0;
var mediaIndex = 0;
var mediaInterval;
var stopRotation = 0;
var imgWidth = 0;
var slideSwitchInterval = 10000;
var newsSlideInterval = 8000;


// TOUCH-EVENTS SINGLE-FINGER SWIPE-SENSING JAVASCRIPT
// Courtesy of PADILICIOUS.COM and MACOSXAUTOMATION.COM
// this script can be used with one or more page elements to perform actions based on them being swiped with a single finger

var triggerElementID = null; // this variable is used to identity the triggering element
var fingerCount = 0;
var startX = 0;
var startY = 0;
var curX = 0;
var curY = 0;
var deltaX = 0;
var deltaY = 0;
var horzDiff = 0;
var vertDiff = 0;
var minLength = 72; // the shortest distance the user may swipe
var swipeLength = 0;
var swipeAngle = null;
var swipeDirection = null;

$(function() {
    //initialize
    setTimeout("initSlider()", 400);
    newsRotate();
    //setInterval(newsRotate,6000);
    setInterval(newsRotate, newsSlideInterval);
    setPaging(0);
    //$("#spnRT").html(mediaJson[0].Caption);
    loadMainRotator(0);
    mediaInterval = setInterval(mediaRotator, 2000);
    $("#divPaging").hover(function() {
        clearInterval(mediaInterval);
    }
        );

});

function initSlider() {
    //imgWidth = parseInt($('#slideshow A:first').outerWidth()) + 20;
    imgWidth = $('#slideshow').width();

    $('#slideshow A').each(function() {
        $(this).addClass('inactive');
        $(this).css("width", "960px");        
        $(this).css("left", imgWidth+"px");
    });
    $('#slideshow A:first').css("left", "0px");
    $('#slideshow A:first').removeClass('inactive');
    $('#slideshow A:first').addClass('active');



    // Seting Slider Bottom bar
    var divSliderLeft = $('#slideshow').offset().left;
    var divSliderTop = $('#slideshow').offset().top;

    $('.slideBottomBar').each(function() {
        $(this).css("left", "0px");
        $(this).css("top", 346 + "px");
        $(this).width($('#slideshow').width()-20);
        $(this).css("display", "block");
    });
}

function GetRSSNews() {

    /*
    $.ajax({
    type: "GET",
    url: 'feeds.feedburner.com/aareas',
    dataType: "html",
    success: function(data) {
    readRSS(data)
    },
    error: function(a, b, c) {
    debugger;
    var v = a;
    }
    });
    */

    $.get('feeds.feedburner.com/aareas', function(data) {
        readRSS(data.reponseXML);
    });
}

function readRSS(data) {

}

function newsRotate() {
    //    try{
    if (newsJson[newsIndex]) {
        $("#spNewsHead").html("<a target=\"_New\" href='" + newsJson[newsIndex].NewsLink + "'>" + newsJson[newsIndex].NewsHead + "</a>" + ((newsIndex<3)?"&nbsp;&nbsp;<img src='images/new_article.png' style='vertical-align:text-bottom;' />":""));
        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(1000);
            $("#spnRT").fadeOut(1000);
            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(1000);
                $("#oFlashPlayer").fadeIn(1000);
                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()", slideSwitchInterval); });

function slideSwitch() {
    if (stopRotation == 1) return;
    mediaIndex = (mediaIndex + 1) % (mediaJson.length);

    var $active = $('#slideshow A.active');
    if ($active.length == 0) 
    {
        $active = $('#slideshow A:last');
    }
    var $next = $active.next().length ? $active.next() : $('#slideshow A:first');
    //$active.addClass('last-active');$("#spnRT").fadeOut(6000);


    $next.removeClass('inactive');
    $next.addClass('active');
    /*
    .animate({width: 'toggle' }, 1000, function() {
    $active.removeClass('active last-active'); //$("#spnRT").html(mediaJson[mediaIndex].Caption);$("#spnRT").fadeIn(0); 
    $active.addClass('inactive')
    setPaging(mediaIndex);
    });
    */
    //imgWidth = parseInt($next.outerWidth());

    $next.css("left", imgWidth + "px");
    $next.css("width", "960px");
    $next.animate({ "left": "0px" }, 800, "linear");
    $active.css("left", "0px");
    $active.css("width", "960px");
    $active.animate({ "left": -imgWidth + "px" }, 800, "linear");

    $active.addClass('inactive');
    $active.removeClass('active last-active');

    setPaging(mediaIndex);
}

function slideSwitchHover(index) {
    stopRotation = 1;
    mediaIndex = index;

    $('#slideshow A').each(function() {
        $(this).css("left", -imgWidth + "px");
        $(this).removeClass('active last-active');
    });

    $("#div_" + index).removeClass('inactive');
    $("#div_" + index).addClass('active');
    $("#div_" + index).css("left", "0px");

    setPaging(mediaIndex);

}

function navigateTo(url) {
    window.location.href = url;
}

// Return boolean TRUE/FALSE
//function isIPad() {

//    /*
//    return (
//        (navigator.platform.indexOf("iPhone") != -1) ||

//        (navigator.platform.indexOf("iPad") != -1)

//        );
//   */
//    return true;
//}


///// start touch impletementation coding ///
//function touchStart(event, passedName) {
//    // disable the standard ability to select the touched object
//    //event.preventDefault();
//    // get the total number of fingers touching the screen
//    fingerCount = event.touches.length;
//    // since we're looking for a swipe (single finger) and not a gesture (multiple fingers),
//    // check that only one finger was used
//    if (fingerCount == 1) {
//        // get the coordinates of the touch
//        startX = event.touches[0].pageX;
//        startY = event.touches[0].pageY;
//        // store the triggering element ID
//        triggerElementID = passedName;
//    } else {
//        // more than one finger touched so cancel
//        touchCancel(event);
//    }
//}

//function touchMove(event) {
//    event.preventDefault();
//    if (event.touches.length == 1) {
//        curX = event.touches[0].pageX;
//        curY = event.touches[0].pageY;
//    } else {
//        touchCancel(event);
//    }
//}

//function touchEnd(event) {
//    //event.preventDefault();
//    // check to see if more than one finger was used and that there is an ending coordinate
//    if (fingerCount == 1 && curX != 0) {
//        // use the Distance Formula to determine the length of the swipe
//        swipeLength = Math.round(Math.sqrt(Math.pow(curX - startX, 2) + Math.pow(curY - startY, 2)));
//        // if the user swiped more than the minimum length, perform the appropriate action
//        if (swipeLength >= minLength) {
//            caluculateAngle();
//            determineSwipeDirection();
//            processingRoutine();
//            touchCancel(event); // reset the variables
//        } else {
//            touchCancel(event);
//        }
//    } else {
//        touchCancel(event);
//    }
//}

//function touchCancel(event) {
//    // reset the variables back to default values
//    fingerCount = 0;
//    startX = 0;
//    startY = 0;
//    curX = 0;
//    curY = 0;
//    deltaX = 0;
//    deltaY = 0;
//    horzDiff = 0;
//    vertDiff = 0;
//    swipeLength = 0;
//    swipeAngle = null;
//    swipeDirection = null;
//    triggerElementID = null;
//}

//function caluculateAngle() {
//    var X = startX - curX;
//    var Y = curY - startY;
//    var Z = Math.round(Math.sqrt(Math.pow(X, 2) + Math.pow(Y, 2))); //the distance - rounded - in pixels
//    var r = Math.atan2(Y, X); //angle in radians (Cartesian system)
//    swipeAngle = Math.round(r * 180 / Math.PI); //angle in degrees
//    if (swipeAngle < 0) { swipeAngle = 360 - Math.abs(swipeAngle); }
//}

//function determineSwipeDirection() {
//    if ((swipeAngle <= 45) && (swipeAngle >= 0)) {
//        swipeDirection = 'left';
//    } else if ((swipeAngle <= 360) && (swipeAngle >= 315)) {
//        swipeDirection = 'left';
//    } else if ((swipeAngle >= 135) && (swipeAngle <= 225)) {
//        swipeDirection = 'right';
//    } else if ((swipeAngle > 45) && (swipeAngle < 135)) {
//        swipeDirection = 'down';
//    } else {
//        swipeDirection = 'up';
//    }
//}

//function processingRoutine() {
//    var swipedElement = document.getElementById(triggerElementID);
//   

//    if (swipeDirection == 'left') {
//        // REPLACE WITH YOUR ROUTINES
//        mediaIndex++;

//    } else if (swipeDirection == 'right') {
//        // REPLACE WITH YOUR ROUTINES
//        mediaIndex--;
//    } else if (swipeDirection == 'up') {
//        // REPLACE WITH YOUR ROUTINES
//        mediaIndex++;
//    } else if (swipeDirection == 'down') {
//        // REPLACE WITH YOUR ROUTINES
//        mediaIndex--;
//    }

//    mediaIndex = mediaIndex < 0 ? mediaJson.length-1 : mediaIndex;
//    // slide action
//    mediaIndex = (mediaIndex) % (mediaJson.length);
//    slideSwitchHover(mediaIndex);    
//    
//}
///// end touch ////
