$(window).load(function(){

if(setAcctField == true){
	$('#myaccount_remember_acct,#remember_acct_chk').attr('checked','checked');
	$('#myaccount_form_acct,#acct').val(''+setAcctString+'');
}else{
	$('#myaccount_remember_acct,#remember_acct_chk').removeAttr('checked');
};

// handles https check and adds ad tracking script
httpURLchk = window.location.href;
httpURLchk = httpURLchk.substring(0, httpURLchk.indexOf(":"));

if(httpURLchk == 'https'){
	adScript   = document.createElement("script");
	adScript.type  = "text/javascript";
	adScript.defer = "defer"
	adScript.src   = "https://prx.netmng.com/?aid=1189"; 
	document.body.appendChild(adScript);
	$("body").append('<img src="https://secure.leadback.advertising.com/adcedge/lb?site=695501&srvc=1&betr=ecb23224_NDM_cs=[+]38f7c656_NDM_cs[720]&betq=16070=[+]450910[720]" width = "1" height = "1" border = "0">');
}else{
	adScript   = document.createElement("script");
	adScript.type  = "text/javascript";
	adScript.defer = "defer"
	adScript.src   = "http://prx.netmng.com/?aid=1189"; 
	document.body.appendChild(adScript);
	$("body").append("<img src='http://sl-retargeting.adsonar.com/process/roi/leadback.jsp?lb=68433941591' height='1' width='1' border='0' /><img src='http://sl-retargeting.adsonar.com/process/roi/leadback.jsp?lb=68233940429' height='1' width='1' border='0' /><img src='http://leadback.advertising.com/adcedge/lb?site=695501&srvc=1&betr=ecb23224_NDM_cs=[+]38f7c656_NDM_cs[720]&betq=16070=[+]450910[720]' width = '1' height = '1' border = '0'>");
};

// handles the removal of page preloading screen
$('.page_loading').fadeOut();
$('.main_wrapper').show();

// handles preloading of navigation bg images 
$.fn.preload = function(){
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}
$(['images/drop_nav/main_menu_bg.png','images/drop_nav/menu_item_bg.png','images/drop_nav/menu_bg.png']).preload();
/////////////////////////////////////////////

// checks to see if user is currently logged in to myXclub 
checkLogin();

// FORM SUBMISSION FUNCTIONALLITY
$('#xclub_sign_in_form').submit(function(e) {
		$('#xclub_sign_in_btn').css('background-image','url(../images/ui/xclub_sign_in_active.gif)');
		$('#header_error').html('');
		e.preventDefault();
		if($('#acct').val() == ''){
			$('#xclub_sign_in_btn').css('background-image','url(../images/ui/xclub_sign_in_btn.png)');
			$('#header_error').html('enter your account number');
		}else if ($('#pin').val() == ''){
			$('#xclub_sign_in_btn').css('background-image','url(../images/ui/xclub_sign_in_btn.png)');
			$('#header_error').html('enter your pin number');
		}else{
		var tmpdta = $(this).serialize();
		$.ajax({
			type: 'POST',
			url: '/include/dta_access.php?action=authenticate_patron',
			dataType: 'xml',
			data: $(this).serialize(),
			success: function(data) {
				if ($(data).find('success').text() =='true'){
					$('.signup').show();
					$('#header_error').html('');
					$('#xclub_sign_in_btn').css('background-image','url(../images/ui/xclub_sign_in_btn.png)');
					setAnumCook();
					window.location = 'myaccount.php'
				}else if ($(data).find('success').text() =='false'){
					$('.signup').hide();
					$('#header_error').html('incorrect account or pin #');
					$('#xclub_sign_in_btn').css('background-image','url(../images/ui/xclub_sign_in_btn.png)');
				}else{
					$('#xclub_sign_in_btn').css('background-image','url(../images/ui/xclub_sign_in_btn.png)');
					$('.signup').hide();
					$('#header_error').html('my.Xclub currently unavailable');
				};
			}
		});
	};
});

$('#myaccount_xclub_sign_in_form').submit(function(e) {
		$('#myaccount_sign_in_btn').css('background-image','url(../images/myaccount/preloader-red.gif)');
		$('#logged_out_error').html('');
		e.preventDefault();
		if($('#myaccount_form_acct').val() == ''){
			$('#myaccount_sign_in_btn').css('background-image','url(../images/myaccount/myaccount_sign_in_btn.png)');
			$('#logged_out_error').html('enter your account number');
		}else if ($('#myaccount_form_acct').val() == 'Xclub account number'){
			$('#myaccount_sign_in_btn').css('background-image','url(../images/myaccount/myaccount_sign_in_btn.png)');
			$('#logged_out_error').html('enter your account number');
		}else if ($('#myaccount_form_password').val() == ''){
			$('#myaccount_sign_in_btn').css('background-image','url(../images/myaccount/myaccount_sign_in_btn.png)');
			$('#logged_out_error').html('enter your pin number');
		}else{
		var tmpdta = $(this).serialize();
		$.ajax({
			type: 'POST',
			url: '/include/dta_access.php?action=authenticate_patron',
			dataType: 'xml',
			data: $(this).serialize(),
			success: function(data) {
				if ($(data).find('success').text() =='true'){
					$('.signup').show();
					$('#logged_out_error').html('');
					$('#myaccount_sign_in_btn').css('background-image','url(../images/myaccount/myaccount_sign_in_btn.png)');
					setAnumCook();
					window.location = 'myaccount.php'
				}else if ($(data).find('success').text() =='false'){
					$('.signup').hide();
					$('#logged_out_error').html('incorrect account or pin #');
					$('#myaccount_sign_in_btn').css('background-image','url(../images/myaccount/myaccount_sign_in_btn.png)');
				}else{
					$('#myaccount_sign_in_btn').css('background-image','url(../images/myaccount/myaccount_sign_in_btn.png)');
					$('.signup').hide();
					$('#logged_out_errorr').html('my.Xclub currently unavailable');
				};
			}
		});
	};
});


function setAnumCook(){
	if($('#remember_acct_chk').attr('checked') == 'checked'){
		acctStrng = $('#acct').val();
		var acctNow = new Date();
		var acctExp = new Date(acctNow.getFullYear(), acctNow.getMonth()+1, acctNow.getDate(), 23, 59, 59, 999);
		document.cookie = 'acctRmbr='+acctStrng+'; expires='+acctExp+'; path=/'
	}else if($('#myaccount_remember_acct').attr('checked') == 'checked'){
		acctStrng = $('#myaccount_form_acct').val();
		var acctNow = new Date();
		var acctExp = new Date(acctNow.getFullYear(), acctNow.getMonth()+1, acctNow.getDate(), 23, 59, 59, 999);
		document.cookie = 'acctRmbr='+acctStrng+'; expires='+acctExp+'; path=/'
	}; 
};






// function that checks if user is logged in
function checkLogin(){
		$("#user_name").html('');
		$.ajax({
			type: 'GET',
			url: '/include/dta_access.php?action=get_patron_info',
			dataType: 'xml',
			data: $(this).serialize(),
			success: (function(data) { 
			if ($(data).find('success').text() =='true'){
				$('.myaccount_page_box').hide();
				$('#xclub_login, #xclub_join, .offers_btn').hide();
				$('#xclub_logged_in').show();
				$("#user_name").append('<p>welcome '+$(data).find("first_name").text()+'!</p>');
				$("#user_name").css({'position':'absolute','visibility':'hidden',
    			'display':'block'});
				setTimeout(function(){
				usrBoxWid = $("#user_name").width();
				$("#user_name").css({'position':'static','visibility':'visible',
    			'display':'none'});
				$("#user_name").hide();
				$("#user_name_contain").animate({width:usrBoxWid},function(){$("#user_name").fadeIn();})
				$('body').append('<img src="https://prx.netmng.com/pixel/?aid=1189&tax=sgnupex" width="1" height="1" border="0" />');
				},500);
			}else if($(data).find('success').size() == 0){
				$('.myaccount_menu_wrapper').addClass('notLoggedIn');
				$('.myaccount_page_box').show();
				$('#xclub_logged_in').hide();
				$('#xclub_login').show();
				setTimeout(function(){
				$('.offers_btn').fadeIn(600);
				},2800);
			};
			})
		});
};

// remedies the need for double clicking hovered elements in iOS 
$('a').live('touchend', function(e) {
    var el = $(this);
    var link = el.attr('href');
    window.location = link;
});

// detects parents of select nested divs
whichWrapper = $('.menu_wrapper').parents(':eq(1)').attr('id');
whichPgClass = $('#'+whichWrapper+'').parent().attr('class');

// initial sizing of blackout screen
sizeBScreen();

// resizing of blackout screen and menu modal when browser window adjusted
$(window).resize(function(){
	sizeBScreen();
});

// resizing of blackout screen and menu modal when browser window is scrolled
$(document).scroll(function(){
	sizeBScreen();
});

// function that excutes blackout screen sizing
function sizeBScreen(){
bScreenSize = $(window).height();
scrollDistance = $(window).scrollTop();
$('.blackout_screen').css('height', bScreenSize-96 + scrollDistance);
$('.modal_screen').css('height', bScreenSize + scrollDistance);
};

// hide drop nav initially 
$('.drop_nav, #drop_nav_wrapper').hide();

// leave nav section hide drop menu container and clear image timers
$('#nav_holder').mouseleave(function(){
	$('.blackout_screen, .modal_contain').hide();
	$('#nav li a').removeClass('selected');
	$("#drop_nav_wrapper").stop().fadeTo(200, 0, function(){$(".drop_nav, #drop_nav_wrapper").hide();});
	clearInterval(rtImgTimer);
});

// handles specific nav selection functionality for each catagory 
var navMenTimer;
$('#nav li a').bind('mouseenter',function(){
	clearTimeout(navMenTimer);
	slideOutSub();
	whichList = this.id;
	$(this).addClass('hovered');	
	$('#nav li a').removeClass('selected');
	if($(this).hasClass('drop') && $(this).hasClass('hovered')){
			$(this).addClass('selected');
			navMenTimer = setTimeout(function(){
			if($('#nav li a').hasClass('hovered')){
			$('.drop_nav').stop().show();
			$('#drop_nav_wrapper').stop().fadeTo(100,1).show();
			$('.blackout_screen').stop().fadeTo(100,0.75).show();};},200);
	}else{
			clearTimeout(navMenTimer);
			$('.blackout_screen').stop().fadeTo(100, 0).hide();
			$('#nav li').removeClass('selected');
			$("#drop_nav_wrapper").stop().fadeTo(100, 0, function(){$("#drop_nav_wrapper").hide();})	
	};
 	$("#drop_nav_wrapper").load('drop_menus/'+whichList+'_menu.php', function(){$('#drop_nav_sub_menu,#drop_nav_sub_img, #drop_nav_hero ul').stop().children().hide();dropMenuControls();detectIpad();});
});

function detectIpad(){
if((navigator.platform.indexOf("iPad") != -1)){
	
}else{
	$('#drop_nav_menu>ul>li>a').bind('click',function(){
	whchLink = this.hash;
	if(whchLink == '#!'){}else{
	whchLink = this.hash.replace(/#!\//,'');
	window.open('http://www.parxcasino.com/'+whchLink+'.php','_self');	
	};
	});
};
};

// restores sprite default state when mouse leaves
$('#nav li a').mouseleave(function(){
	$(this).removeClass('hovered')
});

// functionality for drop menu, sub menu, and right side image 
function dropMenuControls(){
			cycleHeroImg();
			$('#drop_nav_menu>ul>li>a').mouseenter(function() {
			slideInSub();
			whichSub = this.id;
			setTimeout(function(){
				if($('#drop_nav_sub_menu').hasClass('subDelay')){
	
				}else{
					if(!$('#'+whichSub+'_sub').is(':animated')){
						if($('#'+whichSub+'_sub').is(':visible')){
						
						}else{
							$('#drop_nav_sub_menu,#drop_nav_sub_img').children().hide();
							$('#'+whichSub+'_sub').stop().fadeIn(300);
							rghtImgCycle();
							cycleHeroImg();
							};
						};
						};
			},400);
			});
			
			/*
			$('#drop_nav_menu>ul>li>a').bind('click',function(){
				window.location = ''+whichSub+'.php';
			});
			*/	
			
			
			$('.menu_end').mouseenter(function(){
				slideOutSub();
			});
			
			$('#drop_nav_sub_menu, #drop_nav_sub_img').mouseenter(function(){
				$(this).addClass('subDelay');
				slideInSub();
			});
			$('#drop_nav_sub_menu, #drop_nav_sub_img').mouseleave(function(){
				$(this).removeClass('subDelay');
			});
};

// cycles the hero image for each section with drop down menu 
var rtHerTimer;
function cycleHeroImg(){
	clearInterval(rtHerTimer);
	var hC = 1;
	subHeroCnt = $('#drop_nav_hero>ul li').size();
	
	if(subHeroCnt > 1){
		$('#drop_nav_hero>ul li:first').stop().show();
		rtHerTimer = setInterval(rghtHerFade, 3000);
	}else{
		clearInterval(rtHerTimer);
		$('#drop_nav_hero>ul li').stop().fadeIn().show();
	};

	function rghtHerFade(){
		hC ++;
		if(hC <= subHeroCnt){
			$('#drop_nav_hero>ul li').hide();
			$('#drop_nav_hero>ul li:nth-child('+hC+')').stop().fadeIn(500).show();
		}else{
			$('#drop_nav_hero>ul li').hide();
			$('#drop_nav_hero>ul li:first').stop().fadeIn(500).show();
			hC = 1;
		};
	};
};

// cycles right side images for each catagory based on user selection
var rtImgTimer;
function rghtImgCycle(){
	clearInterval(rtImgTimer);
	var iA = 1;
	rghtImgCnt = $('.'+whichSub+'_img>li').size();
	$('.'+whichSub+'_img').show().children().hide();
	
	if(rghtImgCnt > 1){
		$('.'+whichSub+'_img li:first').stop().fadeIn().show();
		rtImgTimer = setInterval(rghtFade, 3000);
	}else{
		clearInterval(rtImgTimer);
		$('.'+whichSub+'_img li').stop().fadeIn().show();
	};

	function rghtFade(){
		iA++;
		if(iA <= rghtImgCnt){
			$('.'+whichSub+'_img li').hide();
			$('.'+whichSub+'_img li:nth-child('+iA+')').stop().fadeIn(500).show();
		}else{
			$('.'+whichSub+'_img li').hide();
			$('.'+whichSub+'_img li:first').stop().fadeIn(500).show();
			iA = 1;
		};
	};
};

// slides in the sub menu and right side image
function slideInSub(){
	$("#drop_nav_hero").stop().fadeTo(200, 0, function(){$("#drop_nav_hero").hide();});
	$("#drop_nav_sub_menu").stop().animate({left: 248}, 500, function(){$("#drop_nav_sub_img").stop().fadeTo(200,1)});
};

// slides out the sub menu and right side image
function slideOutSub(){
	$("#drop_nav_sub_img").stop().fadeTo(200, 0).hide();
	$("#drop_nav_hero").stop().fadeTo(500, 1).show();
	$("#drop_nav_sub_menu").stop().animate({left: 0}, 500);
};

var xclubTime; 
$('.xclub_sign_in_drop, #xclub_login, .myxclub_drop, #xclub_logged_in').hover(function(){ 
    	clearInterval(xclubTime);
		$(this).addClass('mouseIn');
    }, function(){ 
		$(this).removeClass('mouseIn');
		xclubTime = setInterval(bodyCloseCond, 1000);
    });

// even handler for xclub log in click
$('.help_desk_number').hide();
$('#xclub_login').bind('click',function(){
	$('.myaccnt_error').html('');
	$('#xclub_sign_in_btn').css('background-image','url(../images/ui/xclub_sign_in_btn.png)');
	$('.xclub_sign_in_drop').stop().fadeIn().show();
});



$('#xclub_logged_in').bind('click',function(){
	$('.myxclub_drop').stop().fadeIn().show();
});

$('.forgot').bind('click',function(){
	$('#xclub_sign_in_form').fadeOut();
	setTimeout(function(){$('.help_desk_number').fadeIn();},500);
});

$('.return_login').bind('click',function(){
	$('.help_desk_number').fadeOut();
	$('#xclub_sign_in_form').fadeOut();
	setTimeout(function(){$('#xclub_sign_in_form').fadeIn();},500);
});

// condition that checks mouse is on log in menu or outside and fades out appropriately
function bodyCloseCond(){
		if($('.xclub_sign_in_drop, #xclub_login, .myxclub_drop, #xclub_logged_in').hasClass('mouseIn')){
		
		}else{
			$('.xclub_sign_in_drop, .myxclub_drop').fadeOut();
		};
};

//boxColHeight = $('.box_container').height();
//$('.box_body').css('height',6+boxColHeight);

$('.dining_menu').bind('click',function(){
mP=0;
menStrng = $(this).attr('href');
whchMenu = menStrng.replace('#!/','');
$(".menu_modal_contain").css({left:0});
$('.menu_modal_contain').load('../dining_menus/dining_menus.php .'+whchMenu+'_menu',function(response, status, xhr){
	if (status == "success") {
   	setInterval(function(){
	menuPgAmount = $('ul.'+whchMenu+'_menu').children().size();
	menuPgWidth = $('.'+whchMenu+'_menu>li').width();
	menuPgHeight = $('.'+whchMenu+'_menu>li').height();
	$('.menu_modal_viewer').css('width', menuPgWidth);
	$('.menu_modal_contain').css('width', menuPgWidth * menuPgAmount);
	$('.menu_modal_contain, .menu_modal_viewer').css('height', menuPgHeight);},200);
  	$('.menu_modal_left,.menu_modal_left').css('background-position', '0px 0px');
	if(whchMenu == 'parxgrill_reservations'){$('.menu_modal_controls').hide();};
	};
});
setTimeout(function(){$('.menu_modal').fadeIn(300);},200);
});

$(document).keyup(function(e) {
    if (e.keyCode == 27) {$('.menu_modal').fadeOut();};
	if (e.keyCode == 39) {dinMenRight();};
    if (e.keyCode == 37) {dinMenLeft();}; 
});

$('.menu_modal_contain,.menu_modal_controls').mouseenter(function(){
		$(this).addClass('mouseIn');
		$('.menu_modal').unbind();
});

$('.menu_modal_contain,.menu_modal_controls').mouseleave(function(){
		$(this).removeClass('mouseIn');
		$('.menu_modal').bind('click',function(){
		if($('.menu_modal_wrapper,.menu_modal_controls').hasClass('mouseIn')){}else{$(this).stop().fadeOut(300);};
		});
});
	
$('.menu_modal_right').bind('click',function(){
	dinMenRight();	
});

$('.menu_modal_left').bind('click',function(){
	dinMenLeft();
});

function dinMenRight(){
	mP++;
	if (mP <= menuPgAmount-1){
		$(".menu_modal_contain").animate({left: -menuPgWidth * mP},{duration:1000 ,easing:'easeInOutElastic'});
		$('.menu_modal_left').css('background-position', '0px 0px');
	}else{
		mP--;
	};
	if(mP == menuPgAmount - 1){
		$('.menu_modal_right').css('background-position', '0px -100px');
	};
};

function dinMenLeft(){
	mP--;
	if (mP > 0){
		$(".menu_modal_contain").animate({left: -menuPgWidth * mP},{duration:1000 ,easing:'easeInOutElastic'});
		$('.menu_modal_right').css('background-position', '0px 0px');
	}
	else{
		$('.menu_modal_left').css('background-position', '0px -100px');
		$(".menu_modal_contain").animate({'left':0},{duration:1000 ,easing:'easeInOutElastic'});
		mP=0;
	};
};

$('a.menu_modal_download').bind('click',function(){
	menPath = whichPgClass.replace('_page','');
	window.open('http://www.parxcasino.com/dining_menus/'+menPath+'/'+whchMenu+'_menu.pdf');
});


// HERO IMAGE CYCLE FUNCTIONALITY \\	
$('.hero_pager_list li').bind('click',function(){
		clearInterval(heroIMGTimer);
		numPos = $(this).index();
		cntrlFunc();
});

	function cntrlFunc(){
		numNext = numPos+1;
		heroImgCycle();
		$('.hero_pager_list li').css('background-image','url(../images/home/pager_btn.png)');
		$('.hero_pager_list li:nth-child('+numNext+')').css('background-image','url(../images/home/pager_sel_btn.png)');		
	};
	
	cycState = 2;
	function heroImgCycle(){
		$('.hero_pager_list li').unbind();
		if(cycState == 1){
		$('.hero_pager_list li').css('background-image','url(../images/home/pager_btn.png)');
		$('.hero_pager_list li:nth-child('+numNext+')').css('background-image','url(../images/home/pager_sel_btn.png)');
		$('#home_hero').css('background-image','url(images/home/0'+numNext+'.jpg)');
		$('#home_hero_aux').fadeTo(500,0,function(){
		$('#home_hero').css('z-index', '1');
		$('#home_hero_aux').css('z-index', '0');
		$('#home_hero_aux').fadeTo(0,1,function(){$('.hero_pager_list li').bind('click',function(){numPos = $(this).index();cntrlFunc();});});
		cycState = 2;
		});
		}else{
		$('.hero_pager_list li').css('background-image','url(../images/home/pager_btn.png)');
		$('.hero_pager_list li:nth-child('+numNext+')').css('background-image','url(../images/home/pager_sel_btn.png)');
		$('#home_hero_aux').css('background-image','url(images/home/0'+numNext+'.jpg)');
		$('#home_hero').fadeTo(500,0,function(){
		$('#home_hero_aux').css('z-index', '1');
		$('#home_hero').css('z-index', '0');
		$('#home_hero').fadeTo(0,1,function(){$('.hero_pager_list li').bind('click',function(){numPos = $(this).index();cntrlFunc();});});
		cycState = 1;
		});
		};
	};
	
	numNext = 1;
	$('.hero_pager_list li:nth-child(1)').css('background-image','url(../images/home/pager_sel_btn.png)');
	heroIMGTimer = setInterval(runHeroSlide, 5000);
	function runHeroSlide(){
		if(numNext >= 8){
		numNext = 0;
		numNext ++;
		heroImgCycle();
		}else{
		numNext ++;
		heroImgCycle();
		};
	};
	
	$('.hero_pager_contain').mouseenter(function(){
		clearInterval(heroIMGTimer);
	});
	$('.hero_pager_contain').mouseleave(function(){
		heroIMGTimer = setInterval(runHeroSlide, 5000);
	});

});
