$(document).ready(function()
{
	// show active navigation tab
	$("#main_nav a").each(function() {
		var href_uri = $(this).attr("href");
		if (href_uri.toLowerCase() == location.href.toLowerCase()) {
			$(this).addClass("active");
		}
	});
		
	$('.toggle_hide').click(function() {
		$('.toggle_content').hide();
		$('span.promotion_detail').html('');
		$('.normal_content').show();
	});
	
	// password fields swap
	$('.fnfield_swap').click(function() {
		$('.fnfield_swap').val("");
		$('span.fnpassword_field').html('<input type="password" class="txt50 required" name="password" id="password" />');
		$('.fnfield_swap').focus();	
	});
	
	$('.lsfield_swap').click(function() {
		$('.lsfield_swap').val("");
		$('span.lspassword_field').html('<input type="password" class="required" name="password" tabindex="2" id="password" />');
		$('.lsfield_swap').focus();	
	});

	// show/hide player inf
	$('.show_player_inf').click(function() {
		$('.player_info_back_exp').fadeOut(function() {
			$('.player_info_back').fadeIn();
		});
	});
	
	$('.hide_player_inf').click(function() {
		$('.player_info_back').fadeOut(function() {
			$('.player_info_back_exp').fadeIn();
		});
	});
		
	// init reg validation
	$(".validationForm").validate({
		errorClass: "displayError",
		rules: {
			alias: {
				rangelength: [5, 25],
				maxlength: 25,
				remote: {
					url: "register/check_alias",
					type: "post",
					async: false,
					beforeSend: function()
					{
						$('#alias').attr('readonly', 'readonly');
						$('#alias').addClass('loading_field');
					},
					complete: function()
					{
						$('#alias').removeAttr('readonly', 'readonly');
						$('#alias').removeClass('loading_field');
					}
				}
			},
			email: {
				required: true,
				email: true
			}
		}
	});
	
	// init login validation
	$(".login_validation").validate({
		errorClass: "displayError"
	});
	
	// reg second part validation
	$(".reg_validation").validate({
		errorClass: "displayError"
	});
	
	// init accordian
	$(".div_expand").accordion({
		header: 'h4',
		autoHeight: false
	});
	
	// goto register
	$(".start_register").click(function() {
		window.location = base_url + "/members/register";
	});
	
	// goto casino
	$(".casino").click(function() {
		window.location = base_url + "/games/allgames";
	});
	
	// goto login
	$(".login").click(function() {
		window.location = base_url + "/login";
	});
	
	// submit ecom iframe form
	$('.ecomlogin').submit();
	
	$("ul.sf-menu").supersubs({ 
        minWidth:    10,   // minimum width of sub-menus in em units
        maxWidth:    27,   // maximum width of sub-menus in em units
        extraWidth:  1     // extra width can ensure lines don't sometimes turn over
                           // due to slight rounding differences and font-family 
    }).superfish({
    	hoverClass:'sfHover',
		delay: 500,
		speed: 'fast', 
        autoArrows: false, 
        dropShadows: false,
        disableHI: true
	}).find('ul').bgIframe({opacity:false});;

	$("table.report tr:even").addClass("even");
	$("table.report tr:odd").addClass("odd");

	$(".checkBoxTree").each(function(){          
		addTreeView($(this), "", "", false, false);
		$(this).TreeViewCheckboxes();              
		$(this).show();                            
	});
	
	$("select.sort").each(function() {
	    var selectedVal = $(this).val();
		$(this).sortOptions(true);
		$(this).val(selectedVal);
	});

});

function initDataTables(localeFile) {
	$(".report").each(function() {
		if (!$(this).hasClass("notDynamic")) {
			$(this).dataTable({
				"bPaginate":true,
				"bLengthChange":false,
				"bFilter":false,
				"bSort":true,
				"bInfo":true,
				"bProcessing":true,
				"bAutoWidth":true,
				"iDisplayLength":25,
				"sPaginationType":"full_numbers",
				"oLanguage": {
					"sUrl": localeFile
				}
			});
		}
	});
}

function parseXML( xml ) {
	if( window.ActiveXObject && window.GetObject ) {
		var dom = new ActiveXObject( 'Microsoft.XMLDOM' );
		dom.loadXML( xml );
		return dom;
	}
	if( window.DOMParser ) {
		return new DOMParser().parseFromString( xml, 'text/xml' );
	}
	throw new Error( 'No XML parser available' );
} 

jQuery.parseXML = function( xml ) {
   	return jQuery( parseXML(xml) );
};

function addTreeView(element, animation, persist, collapsed, unique) {
	$(element).treeview({                                               
		animated: animation,                                              
		persist: persist,                                                 
		collapsed: collapsed,                                             
		unique: unique                                                    
	});                                                                 
}          

function fn_ajaxPromo(uri)
{
	// show ajax box with loader
	$('.promo-loading-box').show();
	$('.toggle_content').show();
	$('.normal_content').hide();
			
	$.ajax({
		url: base_url + uri,
		type: 'GET',
		success: function(response)
		{
			$('.promo-loading-box').hide();
			$('span.promotion_detail').html(response);
		}
	});
}

/* Check user alias exists */
function checkAliasExists(alias) 
{
	var alias = alias.value;
	
	$('#alias').attr('readonly', 'readonly');
	$('#alias').addClass('loading_field');
	
	$.ajax({
		type: "POST",
		async: false,
		url: base_url + "/members/register/check_alias",
		data: {selectedAlias:alias},
		success: function(msg)
		{
			$('#alias').removeAttr('readonly', 'readonly');
			$('#alias').removeClass('loading_field');
			
			if (msg.indexOf('found') == -1)
			{
				$("#alias").removeClass("displayError");
				$(".alias_check").hide();
			}
			else
			{
				$(".alias_check").show();
				$(".alias_check").html(jQuery.trim(msg));
				$("#alias").addClass("displayError");
			}
			
		}
	});
}

// Setup the report datepickers
$(function() {
	$("#hiddenStartDate").datepicker({
		showOn: 'button',
		dateFormat: "m/d/yy",
		buttonImage: common_url + '/images/icons/calendar.jpg',
		buttonImageOnly: true,
		showAnim: "fadeIn",
		onSelect: function(dateText, inst) {
		
			// explode date
			var dateparts = dateText.split("/");

			// set date dropdowns
			$('#startMonth').val(dateparts[0]);
			$('#startDay').val(dateparts[1]);
			$('#startYear').val(dateparts[2]);
		}
	});
});

$(function() {
	$("#hiddenEndDate").datepicker({
		showOn: 'button',
		dateFormat: "m/d/yy",
		buttonImage: common_url + '/images/icons/calendar.jpg',
		buttonImageOnly: true,
		showAnim: "fadeIn",
		onSelect: function(dateText, inst) {
		
			// explode date
			var dateparts = dateText.split("/");

			// set date dropdowns
			$('#endMonth').val(dateparts[0]);
			$('#endDay').val(dateparts[1]);
			$('#endYear').val(dateparts[2]);
		}
	});
});

/* quick date select using fixed date */
function jQuery_setFixedDate()
{
	var fixedVal = $('#fixedDate').val();
	
	if (fixedVal != 0)
	{
		var dt = new Date();
		var timestamp = dt.getTime() - fixedVal * 24 * 60 * 60 * 1000;
		
		var newDate = new Date(timestamp);
	
		// Set the date fields
		$('#startMonth').val(newDate.getMonth() + 1);
		$('#startDay').val(newDate.getDate());
		$('#startYear').val(newDate.getFullYear());
	}
}

/* ajax request */
function submitAjaxRequest(theForm, responseCont)
{
	$('#' + theForm).ajaxSubmit({
				
		success: function(responseXML) {
			
			// post ecom redirect form
			$('#' + responseCont).html(responseXML);

		}
	});
}