var pagesInfo = [
{
	title:"The Experiance",
	barwidth: 123,
	handpos: 137,
	page: "subpage.aspx?pageid=16"
},
{
	title:"The Mentor",
	barwidth: 225,
	handpos: 253,
	page: "subpage.aspx?pageid=7"
},
{
	title:"Events",
	barwidth: 295,
	handpos: 339,
	page: "subpage.aspx?pageid=20"
},
{
	title:"In The Media",
	barwidth: 406,
	handpos: 430,
	page: "subpage.aspx?pageid=8"
},
{
	title:"Photo Gallery",
	barwidth: 528,
	handpos: 546,
	page: "subpage.aspx?pageid=32"
},
{
	title:"Private Events",
	barwidth: 655,
	handpos: 671,
	page: "subpage.aspx?pageid=10"
}
];

/////////////////////////////
var max_menu_bar_width = 732;
bg_block_width = 1300;
var local_link_formats_allowed = ["jpg", "jpeg", "png", "gif", "xls", "xlsx", "pdf", "doc", "docx", "ashx", "com"];
/////////////////////////////

var content_timeout=-1;
var last_page_index = null;
var at_home = true;
var prev_requested_path = "";

function onWindowResize() {
	var w = ($(window).width() - $("#page").width()) / 2;
	$("#page").css({
	   left:w+"px"
	});
	$("#mainbg").height($(window).height());
	
	if(last_page_index != null) {
	   moveBg(last_page_index);
	}
}
$(window).resize(onWindowResize);
$(document).ready(onWindowResize);

function showLoader (show) {
    if(show) {
        $("<div>").attr("id", "loader_spinner").appendTo("#header");
    } else {
        $("#loader_spinner").remove();
    }
}


function backToHome() 
{
	// reset bars and hand pos and BG
	last_page_index = -1;
	moveBg(last_page_index);
	$(".menuBg").stop().animate({ width:"1px" }, 800);
	$("#drag_hand").stop().animate({ left:"0px" }, 500);
	
	// remove content
	$("#main").slideUp();
	
	at_home = true;
	prev_requested_path = "";
	document.location.hash = "";
	ShowHideDevelopedBy();
	$('#pane2').jScrollPane({ showArrows: true });
	document.title = 'MusicHall - Home';
}

function activetMenuItem (index)
{
	$('#main').slideUp();
	var obj = pagesInfo[index];
	// bar size
	$(".menuBg").stop().animate({ width:obj.barwidth+"px" }, 800);
	// hand pos
	$("#drag_hand").stop().animate({ left:obj.handpos+"px" }, 500);
	// bg pos
	moveBg(index);
	// load content
	var path = obj.page;
	var title = obj.title;
	clearTimeout(content_timeout);
	content_timeout = setTimeout(function() {
	   loadContent(path);
    }, 2000);
    document.title = 'MusicHall - ' + title;
}

function activetSubMenuItem(index,path) {
    $('#main').slideUp();
    var obj = pagesInfo[index];
    // bar size
    $(".menuBg").stop().animate({ width: obj.barwidth + "px" }, 800);
    // hand pos
    $("#drag_hand").stop().animate({ left: obj.handpos + "px" }, 500);
    // bg pos
    moveBg(index);
    // load content
    clearTimeout(content_timeout);
    content_timeout = setTimeout(function () {
        loadContent(path);
    }, 2000);
}

function moveBg (index){
	var win_w = $(window).width();
	var diff = (win_w - bg_block_width) / 2;
	var bg_pos = -((index + 1) * bg_block_width) + diff
	// centers BG based on initial pos and win size
	$("#bg_mover").stop().animate({ left:bg_pos }, 1500);
	last_page_index = index;
}

function loadContent(path) 
{
    prev_requested_path = path;
    document.location.hash = path;
    at_home = false;
	if($("#main:visible").length) {
    	// hide old content
    	$('#main').slideUp(400, function() {$(this).empty()});
    	// wait then load new
    	setTimeout(function () {
    	    doLoadContent(path);
    	}, 600);
	} else {
	    // nothing is opened
	    doLoadContent(path);
	}
	ShowHideDevelopedBy();
	$('#pane2').jScrollPane({showArrows:true});
}

function doLoadContent (path) 
{
    if(at_home) return;
    showLoader(true); 
    $.get(path, function (data) {
        onHTMLContentLoad(data);
    });
    ShowHideDevelopedBy();
	$('#pane2').jScrollPane({showArrows:true});
}

function onHTMLContentLoad (html) 
{
    if(at_home) return;
    
    $("#main").html($(html).find("#main"));
    $('#main').slideDown(800);
    
    HideLeftMenuIfEmpty();
    
    Cufon.replace('h1#mainPageTitle');
    
    if ($(".eventsListing").length) {
        $(".eventsListing a[rel='example1']").colorbox();
        $(".eventsListing a[rel='example2']").colorbox();
    }
    if ($(".galleryListing").length) {
        $(".galleryListing a[rel='example1']").colorbox();
        $(".galleryListing a[rel='example2']").colorbox();
    }
    if ($(".galleryPicturesListing").length) {
        $(".galleryPicturesListing a[rel='example1']").colorbox();
    }
    
    redirectLinksToJS();
    showLoader(false);
    ShowHideDevelopedBy();
	$('#pane2').jScrollPane({showArrows:true});
}

function redirectLinksToJS() 
{
    // let local links call JS instead of openening page
    $("#main").find("a").each(function() {
        var valid_link = true, href=$(this).attr("href").toLowerCase();
        if(href.indexOf("javascript:") != -1) valid_link = false;
        if(href.indexOf("http://") != -1) valid_link = false;
        for (var i=0; i < local_link_formats_allowed.length; i++) {
            if(href.indexOf("."+local_link_formats_allowed[i]) != -1) {
                valid_link = false;
                break;
            }
        }
        
        if(valid_link) {
            $(this).click(function(ev) {
                loadContent($(this).attr("href"));
                ev.preventDefault();
            })
        }
    });
}

function checkPageHash () {
    var hash = document.location.hash.split("#").join("");
    if(hash != "") {
        // find the current link in the main menu definition and highlight it
        var main_menu_clicked = false;
        for (var i=0; i < pagesInfo.length; i++) {
            if(pagesInfo[i].page.toLowerCase() == hash.toLowerCase()) {
                activetMenuItem(i);
                main_menu_clicked = true;
                break;
            }
        }
        // load the page passed in the hash
        if(!main_menu_clicked) loadContent(hash);
        
    } else {
        backToHome();
    }
}

function initDraggableHand () {
	$("#drag_hand").draggable({
	   containment: "#drag_hand_wrapper",
	   axis: "x",
	   stop: onHandDragStop,
	   drag: moveBarWithHand
	});
}

function moveBarWithHand () {
	var hand_pos = $("#drag_hand").offset().left - $("#drag_hand_wrapper").offset().left;
	var max_hand_pos = $("#drag_hand_wrapper").width() - $("#drag_hand").width();
	var bar_width = (hand_pos * max_menu_bar_width) / max_hand_pos;
	$(".menuBg").stop().css({ width:bar_width+"px" });
}

function onHandDragStop () {
	var hand_pos = $("#drag_hand").offset().left - $("#drag_hand_wrapper").offset().left;
	// find the nearest menu object 
	var positions = [];
	for(var i=0; i<pagesInfo.length; i++){
	   positions.push({
		   index: i,
		   pos: Math.abs(pagesInfo[i].handpos - hand_pos)
	   });
	}
	positions.sort(sortByPos);
	var nearest_index = positions[0].index;
	activetMenuItem(nearest_index);
}

function sortByPos (a,b) {
	if(a.pos > b.pos) {
	   return 1;
	}
	return 0;
}

$(function() {
	$("#menu a").click(function() {
    	var index = $("#menu a").index($(this));
    	activetMenuItem(index);
    });

    initDraggableHand();

    $("#logo").click(backToHome);
	
	$('#main').slideUp();
});


/* Footer Popups */
function slideFooterPopups(popupid,linkid){
	$(popupid).slideToggle();
	$(linkid).toggleClass('active');
}

/* Events Popups */
$(document).ready(function () {
    $('#upcomLink').click(function () {
        var rightPos = $('#upcomevents').css('right')
        if ($('#upcomevents').css('right') == '-280px') {
            $('#upcomevents').stop().animate({ right: 0 }, 1000);
            $('.showHideBtn').addClass('active');
        }
        else {
            $('#upcomevents').stop().animate({ right: -280 }, 1000);
            $('.showHideBtn').removeClass('active');
        }
    });

    redirectLinksToJS();
    checkPageHash();
    ShowHideDevelopedBy();
	$('#pane2').jScrollPane({showArrows:true});
});

/* Main Content Holder */
function showHideContent(){
	$('#contentCont').slideToggle();
	$('#showContentLink').toggleClass('active');
}

function HideLeftMenuIfEmpty() {
    if ($(".hideme").length) {
        $("#leftMenu").hide()
        $("#leftMenuSpacer").hide()
    }
}
function ShowHideDevelopedBy() {
    if (document.location.href.split('#').length > 1) {
        if (document.location.href.split('#')[1].length) {
            $('#divDeveloped').hide()
        }
        else {
            $('#divDeveloped').show()
        }
    }
    else {
        $('#divDeveloped').show();
    }
}
