// JavaScript Document
var hash = window.location.hash;
var menuPath = "";
var toLoad = "";
var pathAry = [];
var timer = 0 ;
var introHash = "#!cinedeck"
var siteFirstTime = true;
var ssFirstTime = true;
var escapedHash;
var windowFocus=true;

$(document).ready(function(){
    $(window).trigger('hashchange');
    //////////////////////////////////////////////////
    //mainMenu click 
    //////////////////////////////////////////////////
    $('#mainMenu a').bind("click", function(evt){
        clearValidate();
    });
    
    //////////////////////////////////////////////////
    //subMenu click 
    //////////////////////////////////////////////////
    $('#subMenu a').bind("click", function(evt){
        clearValidate(); 
    });
    //////////////////////////////////////////////////
    //nav window height
    //////////////////////////////////////////////////
    $(window).resize(function(){
        fixCSS();  
    });
    ////////////////////////////////////////////////
    //Customer care and Where to buy
    ////////////////////////////////////////////////
    $('.slideMenu').find('a').live("click", function(evt){
        customerMenuClicked(this.id.split('-')[1]);
    });
    $('.topbtn').live('click', function(evt){
        customerMenuClicked($('.slideMenu a:first').attr('id').split('-')[1]); 
    });
   
});
//////////////////////////////////////////////////
//hashchange handler
//////////////////////////////////////////////////
$(window).bind("hashchange", function(e){
    // In jQuery 1.4, use e.getState( "url" );
    hash = window.location.hash;
	
	// google 
			var hashGoogle = location.hash.replace('#!','') || '';
			var query_string = location.search || '';
			_gaq.push(['_setAccount', 'UA-10399464-1']);
			_gaq.push(['_trackPageview',location.pathname + query_string  + hashGoogle]);
			//alert(location.pathname +"qString"+ query_string+"hash google"  + hashGoogle);

	if (hash.indexOf('/') != -1) {
        pathAry = hash.split('/');
        menuPath = pathAry[0];
    } else {
        menuPath = hash;
    }
	// index page
    if (!hash || hash == introHash) { 
		if(siteFirstTime){
			$('#contentContainer').css("display", "none");
			intro();
		}else{
			$('#contentContainer').fadeOut(500);
			slideshow(true);
		}
		buildMenu(introHash);
		//return;
    }
	//subpage first time
	else if(menuPath =="#!subpage" && siteFirstTime ==true)
	{
		slideshow(false);
        $('#subMenu').fadeIn();
        $('#socialInfo').fadeIn();
        $('#mainMenu').fadeIn();
        $('#navContainer').css("position", "fixed");
        $('#navContainer').css("left", "0");
	    buildMenu(introHash); 
		buildContent();
	}
	
	//default situation
	else
	{
		slideshow(false);
        $('#subMenu').fadeIn();
        $('#socialInfo').fadeIn();
        $('#mainMenu').fadeIn();
        $('#navContainer').css("position", "fixed");
        $('#navContainer').css("left", "0");
		buildMenu(menuPath);
		buildContent();
    } 
	siteFirstTime =false;
	
});


//////////////////////////////////////////////////		
//activate menu
//////////////////////////////////////////////////
function buildMenu(menuPath){
    checkBrowserType();		
    $("#menu >li >a").each(function(e){
            if ($(this).attr('href') == menuPath) {
                if (!$(this).hasClass("expanded")) {
                    $("#menu > li > a.expanded").not(this).toggleClass("expanded").toggleClass("collapsed").parent().find('> ul').slideToggle("medium");
                    $(this).toggleClass("expanded").toggleClass("collapsed").parent().find('> ul').slideToggle("medium");
                }
            }
     })
    menuHightlight();  
}
////////////////////////////////////////////////
//////google /#!
////////////////////////////////////////////////	

function buildContent(){
    if (window.location.hash) {
		escapedHash = window.location.hash.replace("#!", "");
		$('#contentContainer').fadeIn(1500);
		$("#containerWrapper").load("data.php?_escaped_fragment_=" + escapedHash,function() { showNewContent()} );
		
		function showNewContent(){
			if($('.rotationRX').length){
				
				loadRXRotateImg();
			}
			if($('.rotationEX').length){
				loadEXRotateImg();
				
			}
			fixCSS();	
            window.scrollTo(0,0);
			$('#content').fadeIn(1500);
			}
		}
}

////////////////////////////////////////////////
//////intro function
////////////////////////////////////////////////	


function intro(){
    //	$('#navContainer').hide();
    $('#mainPageContent').hide();
    $('#introText').fadeIn("slow");
    
    var introTimer = setInterval(function(){
        afterIntro();
        clearInterval(introTimer);
    }, 8000);
    
    $('#introText').bind("click", function(evt){
        afterIntro();
        clearInterval(introTimer);
        return false;
    });
}


function afterIntro(){
    $('#introText').fadeOut(500);
    //	$('#navContainer').delay(2000).fadeIn(1000);
    $('#mainPageContent').show();
    $('#navContainer').animate({
        left: '0',
    }, 500, function(){
        $('#subMenu').fadeIn();
        $('#socialInfo').fadeIn();
        $('#mainMenu').fadeIn();
        $('#navContainer').css("position", "fixed");
    });
    
    $('#slideshow').delay(500).fadeIn(300, function(){
        ssFirstTime = false;
        slideshow(true);
    });
    
}



///////////////////////////////////////////////////
//fixed height
//////////////////////////////////////////////////
function fixCSS(){
    var windowH = $(window).height();
    //$('#navContainer').css('height',(windowH+"px"));	
    //$('#navContainer').css('height',($('#contentContainer').height()+"px"));
    if ($('#content').css('margin-top') != undefined) {
        var contentH = ($('#content').height() + Number($('#content').css('margin-top').split('px')[0]));
    }
    else {
        ///slideType page margin top == 0
        var contentH = $('#content').height();
    }
    $('#contentContainer').css('height', (windowH > contentH ? windowH : contentH) + "px");
    ///////////////////////////////////////
    //validate contact Form
    ////////////////////////////////////////
    if ($('#formID').html() != null && $('#formID').html() != 'null') {
        $("#formID").validationEngine({
            promptPosition: "centerLeft"
        });
    }
    //////////////////////////////////////
    ///slideshow
    /////////////////////////////////////
    if ($('.scrollableMain').html() != null && $('.scrollableMain').html() != 'null') {
        $('.scrollableMain').cycle('destroy');
        $('.scrollmenu').remove();
        $('.scrollableMain').after('<div class="scrollmenu"> </div> ').cycle({
            fx: 'fade',
            speed: 2000,
            timeout: 3000,
            pager: '.scrollmenu',
            pagerAnchorBuilder: function(idx, slide){
                return '<a href="#"></a>';
            }
        });
    }
    checkBrowserType();
    
}


function clearValidate(){
    $(".formError").remove();
}



//////////////////////////////////////////////////
//activate when build menu
//////////////////////////////////////////////////
function menuHightlight(){
    if (hash.indexOf('/') != -1) {
    
        pathAry = hash.split('/');
        switch (pathAry[0]) {
            case "#!cinedeck":
                //deal with a in the .gr
                $("#menu li ul.gr a").each(function(e){ ///each function will run through all a in .gr
                    if ($(this).attr('href') == hash ) {
                        //same as hash, superactive
                        $(this).removeClass('superactiveGR').addClass('superactive');
                    }
                    else {
                        //not the same as hash, superactiveGR
                        $(this).removeClass('superactive').addClass('superactiveGR');
                    }
                });
                //deal with other menus
                $("#menu li ul.ex a").removeClass('superactiveEX');
                $('#menu li ul.rx a').removeClass('superactiveRX');
                $("#subMenu ul li a").removeClass('superactive');
                break;
            case "#!cinedeckEX":
                $("#menu li ul.ex a").each(function(e){
                    if ($(this).attr('href') == hash) {
                        $(this).addClass('superactiveEX');
                    }
                    else {
                        $(this).removeClass('superactiveEX');
                    }
                });
                //deal with other menus
                $('#menu li ul.gr a').removeClass('superactive');
                $('#menu li ul.rx a').removeClass('superactiveRX');
                $("#subMenu ul li a").removeClass('superactive');
                break;
            case "#!cinedeckRX":
                $("#menu li ul.rx a").each(function(e){
                    if ($(this).attr('href') == hash ) {
                        $(this).addClass('superactiveRX');
                    }
                    else {
                        $(this).removeClass('superactiveRX');
                    }
                });
                //deal with other menus
                $('#menu li ul.gr a').removeClass('superactiveEX');
                $('#menu li ul.rx a').removeClass('superactive');
                $("#subMenu ul li a").removeClass('superactive');
                break;
            case "#!subpage":
                $("#subMenu ul li a").each(function(e){
                    if ($(this).attr('href') == hash ) {
                        $(this).addClass('superactive');
                    }
                    else {
                        $(this).removeClass('superactive');
                    }
                });
                //removed RX and EX
                $('#menu li ul.gr a').removeClass('superactive');
                $('#menu li ul.ex a').removeClass('superactiveEX');
                $('#menu li ul.rx a').removeClass('superactiveRX');
                break;
        }
    }
    else {
        $("#menu a").each(function(e){
            $(this).removeClass('superactive')
            $(this).removeClass('superactiveEX')
            $(this).removeClass('superactiveRX')
        });
        $("#subMenu a").each(function(e){
            $(this).removeClass('superactive')
        });
        
    }
}

//////////////////////////////////////////////////
//landing page slideshow
//////////////////////////////////////////////////
$(window).blur(function() {
	windowFocus=false;
});
$(window).focus(function() {
	windowFocus=true;  
});

function slideshow(showSS){
    if (showSS) {
		$('#slideshow img').fadeOut(500);
        $('#sldeshowExtend').fadeIn(1500);
        //$('#slideshow img:gt(O)').hide();
        $('#slideshow img').eq(0).fadeIn(1500);
		if(windowFocus){
        timer = setInterval(function(){
            $('#slideshow :first-child').fadeOut(1500).next('img').fadeIn(1500).end().appendTo('#slideshow');
        }, 3000);
		}
    }
    else {
		$('#slideshow img').fadeOut(500);
        $('#sldeshowExtend').fadeOut(500);
		clearInterval(timer);
        
    }
}

//////////////////////////////////////////////////
//customer care page
//////////////////////////////////////////////////

function customerMenuClicked(_targetID){
    var destination = $('#' + _targetID).position().top;
    $("html,body:not(:animated)").animate({
        scrollTop: destination
    }, 1000);
}

//////////////////////////////////////////////////
//detect iPad and small screen
//////////////////////////////////////////////////
function checkBrowserType(){
    if (navigator.userAgent.indexOf("iPad") != -1 || $(window).height() <= 800) {
        $('#mainMenu').css('position', 'relative');
        $('#subMenu').css('position', 'relative').css('margin-top', '100px');
        $('#socialInfo').css('position', 'relative');
        $('#navContainer').css('height', $('#contentContainer').height() + "px").css('position', 'absolute');
    }
    else {
        $('#mainMenu').css('position', 'fixed');
        $('#subMenu').css('position', 'fixed');
        $('#socialInfo').css('position', 'fixed');
        $('#navContainer').css('position', 'fixed');
        ///2011/11/20 added
        $('#navContainer').css('height', $('#contentContainer').height() + "px");
    }
    if (navigator.userAgent.indexOf("iPad") != -1) {
        $('.bg').css('display', 'none');
    }
}







