//////////////////////////////////////slider js////////////////////////////////////
var currentlySelected = 0;
var timer;
function selectHPItem(index)
{
    if (index < $('#controller li').length)
    {
			
        $('.panel').css({ display: "none" });
        $('.panel:eq(' + index + ')').fadeIn(500);
        $('#controller li').removeClass('slide');
        $('#controller li:eq(' + index + ')').addClass('slide');
            
		currentlySelected = index;
			
        clearTimeout(timer);
        timer = setTimeout('selectHPItem("' + (parseInt(currentlySelected) + 1) + '")', 8000);
			
    }else{
        selectHPItem(0);
    }
}

var communitySelected = 0;
var timerc;
function communityIcons(index) {
    if (index < $('img.community').length) {
        $('img.community').css({ display: "none" });
        $('img.community:eq(' + index + ')').fadeIn(700);
        communitySelected = index;
        clearTimeout(timerc);
        timerc = setTimeout('communityIcons("' + (parseInt(communitySelected) + 1) + '")', 2000);

    } else {
        communityIcons(0);
    }
}

var chooseSelected = 0;
var timert;
function chooseIcons(index) {
    if (index < $('img.integrity').length) {
        $('img.integrity').css({ display: "none" });
        $('img.integrity:eq(' + index + ')').fadeIn(700);
        chooseSelected = index;
        clearTimeout(timert);
        timert = setTimeout('chooseIcons("' + (parseInt(chooseSelected) + 1) + '")', 4000);

    } else {
        chooseIcons(0);
    }
}

var findSelected = 0;
var timerf;
function findIcons(index) {
    if (index < $('img.rightloan').length) {
        $('img.rightloan').css({ display: "none" });
        $('img.rightloan:eq(' + index + ')').fadeIn(700);
        findSelected = index;
        clearTimeout(timerf);
        timerf = setTimeout('findIcons("' + (parseInt(findSelected) + 1) + '")', 4000);

    } else {
    findIcons(0);
    }
}

//radio widget
//---
var currentSelectedRadioWidget = 0;
var timerRadioWidget;

function rotateNextRadioWidget(idx) {

    //Set the opacity of all images to 0
    $('ul.radiowidgetshow li').css({ opacity: 0.0 });

    if (idx < $('.radiowidget').length) {
        idx = idx + 1;
        $(".radiowidget").css("visibility", "hidden");
        $("#radiowidgetitem" + idx).css("visibility", "visible");
        $("#radiowidgetitem" + idx).css("opacity", "0");
        $("#radiowidgetitem" + idx).animate({ opacity: 1.0 }, 1000);

        currentSelectedRadioWidget = idx;
        clearTimeout(timerRadioWidget);
        timerRadioWidget = setTimeout('rotateNextRadioWidget(' + idx + ')', 6000);
    }
    else {
        rotateNextRadioWidget(0);
    }
}
//---

//TV widget
//---
var currentSelectedTVWidget = 0;
var timerTVWidget;

function rotateNextTVWidget(idx) {

    //Set the opacity of all images to 0
    $('ul.tvwidgetshow li').css({ opacity: 0.0 });

    if (idx < $('.tvwidget').length) {
        idx = idx + 1;
        $(".tvwidget").css("visibility", "hidden");
        $("#tvwidgetitem" + idx).css("visibility", "visible");
        $("#tvwidgetitem" + idx).css("opacity", "0");
        $("#tvwidgetitem" + idx).animate({ opacity: 1.0 }, 1000);

        currentSelectedTVWidget = idx;
        clearTimeout(timerTVWidget);
        timerTVWidget = setTimeout('rotateNextTVWidget(' + idx + ')', 6000);
    }
    else {
        rotateNextTVWidget(0);
    }
}
//---



var NCF_UI = {};
NCF_UI.arrVisibleElements = new Array();
NCF_UI.iTotalElementsVisible = 4; /* a const number of 4 elements visible at any given time */
NCF_UI.parent_width = 939;
NCF_UI.item_top_bar_height = 30;


//////////////////////////end slider js///////////////////////////////////

////////////////////////move-able and fisheye/////////////////////////////

/* --- Box Operations --- */
/*
$(".draggable").animate({ marginLeft: "-200%;" }, { queue: true, duration: 1500 })
.animate({ marginLeft: "200%;" }, 1500);
*/

///////////////////////////// Begin Animation Helper /////////////////////////////

/* Validates true if the specified element id was found in the element array */
NCF_UI.isInList = function(id) {
	for ( var i = 0; i < this.arrVisibleElements.length; i++ ) {
		if ( this.arrVisibleElements[i] == id )
			return true;
	}
	
	/* not in the list.. */
	return false;
};

/* Adds a element to the element array */
NCF_UI.addToVisibleList = function(id) {
	if ( !this.isInList(id) )
		this.arrVisibleElements.push(id);
};

/* Removes a visible item from the element array */
NCF_UI.removeFromVisibleList = function(id) {
	for ( var i = 0; i < this.arrVisibleElements.length; i++ ) {
		if ( this.arrVisibleElements[i] == id )
			this.arrVisibleElements.splice(i, 1);
	}
};

NCF_UI.UpdateState = function(id, stateEvent) {

    /* lets add or remove items from the list accordingly */
    switch (stateEvent) {

        case "open":
            if (!this.isInList(id)) {
                this.addToVisibleList(id);
            }
            break;

        case "close":
            if (this.isInList(id)) {
                this.removeFromVisibleList(id);
            }
    }

    /* auto arrange all elements in the list now */
    /* if we have less than 4 elements on screen let's re-adjust everything */
    this.iTotalElementsVisible = this.arrVisibleElements.length;

    return;

    //this.arrVisibleElements.sort(function() { return 0.5 - Math.random() });

    //    if (NCF_UI.iTotalElementsVisible <= 8) {
    //        try { $("#" + NCF_UI.arrVisibleElements[0]).animate({ margin: "290px 0 0 10px" }, 350); } catch (e) { }
    //        try { $("#" + NCF_UI.arrVisibleElements[1]).animate({ margin: "290px 0 0 242px" }, 350); } catch (e) { }
    //        try { $("#" + NCF_UI.arrVisibleElements[2]).animate({ margin: "290px 0 0 472px" }, 350); } catch (e) { }
    //        try { $("#" + NCF_UI.arrVisibleElements[3]).animate({ margin: "290px 0 0 704px" }, 350); } catch (e) { }
    //        try { $("#" + NCF_UI.arrVisibleElements[4]).animate({ margin: "160px 0 0 10px" }, 350); } catch (e) { }
    //        try { $("#" + NCF_UI.arrVisibleElements[5]).animate({ margin: "160px 0 0 242px" }, 350); } catch (e) { }
    //        try { $("#" + NCF_UI.arrVisibleElements[6]).animate({ margin: "160px 0 0 472px" }, 350); } catch (e) { }
    //        try { $("#" + NCF_UI.arrVisibleElements[7]).animate({ margin: "160px 0 0 704px" }, 350); } catch (e) { }
    //        
    //        return;
    //        var bottomRow = "220";
    //        var topRow = "90";
    //        var currentRow = bottomRow;
    //        var start = 10;
    //        
    //        for (var i = 0; i < NCF_UI.iTotalElementsVisible; i++) {

    //            //which row?
    //            if (i >= 4) {
    //                currentRow = topRow;
    //                start = 10;
    //            }

    //            try {
    //                $("#" + NCF_UI.arrVisibleElements[i]).animate({ margin: currentRow.toString() + "px 0 0 " + start.toString() + "px" }, 'slow');
    //                start += 232;
    //            }
    //            catch (e) { continue; }
    //        }
    //    }
};

function ShowWindow(id, speed) {

    if ($("#" + id).css('display') == 'none') {
        highestVisibleBoxIndex++;

        $("#" + id).css({ top: Box_Top_Offsets[highestVisibleBoxIndex], left: Box_Left_Offsets[highestVisibleBoxIndex] })

        $("#" + id).show("slow", speed);
        Visible_Box_Ids.push(id);
    }
    

}

function HideWindow(id, speed) {
    highestVisibleBoxIndex--;
    
    var indexOfRemovedItem = Visible_Box_Ids.indexOf(id);

    Visible_Box_Ids.splice(indexOfRemovedItem,1);
    
	$("#" + id).hide(speed);
	$("#" + id).css("z-index", "1000");

	for (var i = indexOfRemovedItem; i < Visible_Box_Ids.length; i++) {
	    
	        $("#" + Visible_Box_Ids[i]).animate({ top: Box_Top_Offsets[i], left: Box_Left_Offsets[i] }, "slow")
	    
	}
	
    //NCF_UI.UpdateState(id, "close");
}





//build parralel array of the offsets that determine the position of each box
var Box_Ids = ["#box_one", "#box_two", "#box_three", "#box_four", "#box_five", "#box_six", "#box_seven", "#box_eight", "#box_nine", "#box_ten"];
var Visible_Box_Ids = ["box_one", "box_two", "box_three", "box_four"];
var Box_Top_Offsets = ["290px", "290px", "290px", "290px", "160px", "160px", "160px", "160px", "160px", "160px"];

var Box_Left_Offsets = ["10px", "242px", "474px", "706px", "10px", "242px", "474px", "706px", "706px", "706px"];
var highestVisibleBoxIndex = 0;



$(document).ready(function() {

    $('#controller li').each(function(i) {
        $(this).click(function() {
            selectHPItem(i);
        });



        /* Auto align the boxes */
        $(Box_Ids[0]).css({ top: Box_Top_Offsets[0], left: Box_Left_Offsets[0] });
        $(Box_Ids[1]).css({ top: Box_Top_Offsets[1], left: Box_Left_Offsets[1] });
        $(Box_Ids[2]).css({ top: Box_Top_Offsets[2], left: Box_Left_Offsets[2] });
        $(Box_Ids[3]).css({ top: Box_Top_Offsets[3], left: Box_Left_Offsets[3] });

        highestVisibleBoxIndex = 3;

        $(Box_Ids[4]).css({ top: Box_Top_Offsets[4], left: Box_Left_Offsets[4], display: "none" });
        $(Box_Ids[5]).css({ top: Box_Top_Offsets[5], left: Box_Left_Offsets[5], display: "none" });
        $(Box_Ids[6]).css({ top: Box_Top_Offsets[6], left: Box_Left_Offsets[6], display: "none" });
        $(Box_Ids[7]).css({ top: Box_Top_Offsets[7], left: Box_Left_Offsets[7], display: "none" });

        $(Box_Ids[8]).css({ top: Box_Top_Offsets[7], left: Box_Left_Offsets[7], display: "none" });
        $(Box_Ids[9]).css({ top: Box_Top_Offsets[7], left: Box_Left_Offsets[7], display: "none" });



        $(".draggable").each(function() {
            if ($(this).css("display") != "none") {
                NCF_UI.addToVisibleList($(this).attr("id"));
            }
        });

        /* initialize a draggable item */
        /*$('.draggable').Draggable({
        zIndex: 1000,
        ghosting: false,
        opacity: 0.7,
        containment: 'parent'
        });*/

        /* initialize the fish-eye menu */
        $('#fisheye').Fisheye(
			{
			    maxWidth: 50,
			    items: 'a',
			    itemsText: 'span',
			    container: '.fisheyeContainter',
			    itemWidth: 40,
			    proximity: 90,
			    halign: 'center'
			})

        $('#fisheye2').Fisheye(
			{
			    maxWidth: 20, /*this sets how much the menu expands*/
			    items: 'a',
			    itemsText: 'span',
			    container: '.fisheyeContainter',
			    itemWidth: 80,  /*sets the width of icons but also makes them larger */
			    proximity: 80,
			    alignment: 'left',
			    valign: 'top',
			    halign: 'center',
			    margin: 0


			})
    });

    selectHPItem(0);

    communityIcons(0);

    chooseIcons(0);

    findIcons(0);

    //radio widget
    //    //---
    //    //Load the slideshow
    rotateNextRadioWidget(0);
    //    //---

    //    //tv widget
    //    //---
    //    //Load the slideshow
    rotateNextTVWidget(0);
    //---

    //Panel Backgrounds
    $('.panel:eq(0)').css('background-image', 'url(/images/House-Slide-10.jpg)');
    $('.panel:eq(1)').css('background-image', 'url(/images/House-Slide-11.jpg)');
    $('.panel:eq(2)').css('background-image', 'url(/images/House-Slide-12.jpg)');
    $('.panel:eq(3)').css('background-image', 'url(/images/House-Slide-13.jpg)');

    //Stopper Image Switcher
    //    $(".stopper").attr("src", "<?php echo get_bloginfo('template_directory'); ?>/images/stopper.gif");
    //    $(".stopper").click(function() {
    //        if (this.src == "<?php echo get_bloginfo('template_directory'); ?>/images/stopper.gif") {
    //            $(".stopper").attr("src", "<?php echo get_bloginfo('template_directory'); ?>/images/player.gif");
    //            clearTimeout(timer);
    //            $('.tmr').stop();
    //        } else {
    //            $(".stopper").attr("src", "<?php echo get_bloginfo('template_directory'); ?>/images/stopper.gif");
    //            selectHPItem(currentlySelected += 1);
    //        }
    //    });

    $(".backarrow").click(function() {
        if (currentlySelected <= 0)
            currentlySelected = 4;
        selectHPItem(currentlySelected -= 1);
    });

    $(".forwardarrow").click(function() {
        if (currentlySelected >= 5)
            currentlySelected = 1;
        selectHPItem(currentlySelected += 1);
    });

    $(".draggable").bind("mouseenter", function(e) {
        var el = $(this).attr("id");
        el = document.getElementById(el);
    });

    $(".draggable").bind("mouseleave", function(e) {
        var el = $(this).attr("id");
        el = document.getElementById(el);
    });

    $(".draggable").bind("mouseup", function(e) {
        var el = $(this).attr("id");
        el = document.getElementById(el);
        $(this).css("z-index", "1000");
    });

    $(".draggable").bind("mousedown", function(e) {
        var el = $(this).attr("id");
        el = document.getElementById(el);
        $(this).css("z-index", "20001");

        
    });

    $(".draggable").draggable({
        cursor: 'move',
        containment: 'parent',
        opacity: 0.85,
        zIndex: 2700
    });
});
