var homeContentIndex = 1;
var homeContentInterval;

// init app
$(function(){

    if (typeof(_gat) != "undefined") {
        var pageTracker = _gat._getTracker("UA-3998558-1");
        pageTracker._initData();
        pageTracker._trackPageview();
    }
	
    setCurrentNavButton();
    
    $("#homeNavStewardship").click(function(){
        stopHomeContentRotation();
        slideHomeContent(1);
    });
    $("#homeNavSecurity").click(function(){
        stopHomeContentRotation();
        slideHomeContent(2);
    });
    $("#homeNavSimplicity").click(function(){
        stopHomeContentRotation();
        slideHomeContent(3);
    });
    $("#homeNavSpeed").click(function(){
        stopHomeContentRotation();
        slideHomeContent(4);
    });
    $("#homeContentPlayPause").click(function(){
        toggleContentRotationPlayback();
    });
    
    //$(".videos a").fancybox({frameWidth:850, frameHeight:670, centerOnScroll:false});
    $(".gallery a").fancybox({
        'zoomOpacity': true,
        'zoomSpeedIn': 250,
        'zoomSpeedOut': 500,
        'overlayOpacity': .5
    });
    
    $(".sample a").fancybox({
        'zoomOpacity': true,
        'zoomSpeedIn': 250,
        'zoomSpeedOut': 500,
        'overlayOpacity': .5
    });
    
	/*
    $("#account a.login").fancybox({
        'zoomOpacity': true,
        'zoomSpeedIn': 250,
        'zoomSpeedOut': 500,
        'overlayOpacity': .5,
        'frameWidth': 304,
        'frameHeight': 125,
        'hideOnContentClick': false,
        'callbackOnShow': loginInit
    });
    */
	
    $('#features a').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        fade: 250,
        top: 10
    });
    
    setTimeout(function(){
        $('#headerKeyTag').cycle({
            fx: 'fade',
            speed: 5000,
            timeout: 12500,
            cleartype: true,
            cleartypeNoBg: true
        });
		
        $("#headerKeyTag").css("position","absolute");
        $("#headerKeyTag").css("top","-8px");
        $("#headerKeyTag").css("right","-20px");
    }, 0);
    
    
    // make sure all the slide images are loaded before starting the animation
    /*
     var imgCount = 0;
     $('img.sliderImg').each(function()
     {
     var image = new Image();
     image.src = $(this).attr('src');
     
     if (image.complete)
     {
     imgCount++;
     
     if (imgCount >= 4)
     homeContentInterval = setInterval(rotateHomeContent, 10000);
     }
     });
     */
    homeContentInterval = setInterval(rotateHomeContent, 10000);
    
});

$.clientCoords = function(){
    var dimensions = {
        width: 0,
        height: 0
    };
    //if (document.documentElement) {
    //    dimensions.width = document.documentElement.offsetWidth;
    //    dimensions.height = document.documentElement.offsetHeight;
    //} else if (window.innerWidth && window.innerHeight) {
    dimensions.width = window.innerWidth;
    dimensions.height = window.innerHeight;
    //}
    return dimensions;
}

function getCurrentPageName(){
    //var page = $.query.get("p");
    var page = window.location.toString().substr(window.location.toString().lastIndexOf("/") + 1);
    return page;
}

function setCurrentNavButton(){
    var page = getCurrentPageName();
    
    if (page == "" || page == "welcome" || page == "stewardship" || page == "security" || page == "simplicity" || page == "speed") {
		$("#topNavWelcome .nav_button_left").toggleClass("nav_button_left_on");
		$("#topNavWelcome .nav_button_right").toggleClass("nav_button_right_on");
	}
	
    if (page == "screenshots") {
		$("#topNavScreenshots .nav_button_left").toggleClass("nav_button_left_on");
		$("#topNavScreenshots .nav_button_right").toggleClass("nav_button_right_on");
	}
	
    if (page == "howto" || page == "techfaq") {
        $("#topNavHowTo .nav_button_left").toggleClass("nav_button_left_on");
        $("#topNavHowTo .nav_button_right").toggleClass("nav_button_right_on");
	}
	
    if (page == "faq" || page == "hardware") {
        $("#topNavFAQ .nav_button_left").toggleClass("nav_button_left_on");
        $("#topNavFAQ .nav_button_right").toggleClass("nav_button_right_on");
	}
	
    if (page == "products" || page == "checkin" || page == "volunteer") {
        $("#topNavProducts .nav_button_left").toggleClass("nav_button_left_on");
        $("#topNavProducts .nav_button_right").toggleClass("nav_button_right_on");
	}
}

function slideHomeContent(idx){
    if (idx == 1) {
        $("#homeContentContainer").animate({
            left: "0px"
        }, 500, "swing");
        $("#homeNavMarker").animate({
            left: "73px"
        }, 500, "swing");
    }
    else 
        if (idx == 2) {
            $("#homeContentContainer").animate({
                left: "-800px"
            }, 500, "swing");
            $("#homeNavMarker").animate({
                left: "273px"
            }, 500, "swing");
        }
        else 
            if (idx == 3) {
                $("#homeContentContainer").animate({
                    left: "-1600px"
                }, 500, "swing");
                $("#homeNavMarker").animate({
                    left: "473px"
                }, 500, "swing");
            }
            else 
                if (idx == 4) {
                    $("#homeContentContainer").animate({
                        left: "-2400px"
                    }, 500, "swing");
                    $("#homeNavMarker").animate({
                        left: "673px"
                    }, 500, "swing");
                }
    
    homeContentIndex = idx;
}

function rotateHomeContent(){
    if (homeContentIndex == 4) 
        homeContentIndex = 1;
    else 
        homeContentIndex++;
    
    slideHomeContent(homeContentIndex);
}

function stopHomeContentRotation(){
    var imgSrc = $("#homeContentPlayPause img").attr("src");
    if (imgSrc.indexOf("pause") > -1) 
        toggleContentRotationPlayback();
    else 
        clearInterval(homeContentInterval);
}

function toggleContentRotationPlayback(){
    var imgSrc = $("#homeContentPlayPause img").attr("src");
    
    if (imgSrc.indexOf("pause") > -1) {
        $("#homeContentPlayPause img").attr("src", "inc/img/play.png");
        clearInterval(homeContentInterval);
    }
    else {
        $("#homeContentPlayPause img").attr("src", "inc/img/pause.png");
        homeContentInterval = setInterval(rotateHomeContent, 10000);
    }
}

function bindWaitCursor(id, posX, posY, offX, offY){
    waitInterval = setInterval(function(){
        var w = $("#" + id).width();// parseInt($("#" + id).css("width"));
        var h = $("#" + id).height(); //parseInt($("#" + id).css("height"));
        var x = $("#" + id).offset().left + offX;
        var y = $("#" + id).offset().top + offY;
        
        if (posX == "right") 
            x += w;
        
        if (posX == "middle") 
            y += (h / 2);
        
        $("#wait").css("top", y + "px").css("left", x + "px").show()
    }, 10);
}

function unbindWaitCursor(){
    clearInterval(waitInterval);
    $("#wait").hide();
}

function setFormErrors(jsonArray){
    $.each(jsonArray, function(index, value){
		var input = $("#" + index);
		var x = input.parent().width() - 16;
		var y = -6;
	
        var errorImg = '<img src="inc/img/error_icon.png" title="' + value + '" class="error_icon" style="position: absolute; top:' + y + 'px;left:' + x + 'px" />';
        $("#" + index).after(errorImg);
    });
}

















