

/*
 *	@ Fortuna Live Betting, Main js Functions
 */

//	@param { string } sectionVars contains site_name (state) and localisation (language) for {section} selection
//	global variables
// box loading overlay (shoved instead of content)
	var loadingOverlay = '<div id="loading_overlay" style="display: block;height: 50px; position: relative;">&nbsp;</div>';
/*
 *	resize static to fit the flash
 *	@method resizeStatic
 *	@param {string} box Id of the content box to resize
 */
function resizeStatic( box ){
	var flashBox = $('#flashid');
	var boxHeight = $(box).height();
	var flashHeight = $('#flashHeight').attr('value');

		if (boxHeight < flashHeight){
			var height = flashHeight + 'px';
			$(flashBox).css('height', height);
		}
		else {
			var height = boxHeight + 50;
			height = height + 'px';
			$(flashBox).css('height', height);
		}
}
/*
 *	content loading function, called when static section loading with ajax, showing the ovelay
 *	@method AJAXoverlay
 *	@param {string} content Url
 */
function AJAXoverlay(content){
	// loading overlay
		$('#static_content').show();
		$('#loading_section').html( loadingOverlay );

	// ajax content load
		$.ajax({
			type: "GET",
			url: content,
			dataType: "html",
			success: function( responseData ){
					$('#loading_section').html( responseData );
					resizeStatic('#static_content');
			}
		});
		return false;
}

/**
 *	results page, ajax article script
 *	@method resultsShow
 *	@param {HtmlElement} result Returns $this, whitch <a> element i clicked
 */
function resultsShow(result){
	var url = $(result).attr('href');
			$('#results').html(loadingOverlay); 
				$.ajax({
					type: "GET",
					dataType: "html",
					url: url,
					success: function( responseData ){
						$('#loading_overlay').hide();
						$('#results_navi a.active').removeClass('active');
						$('#results').html( responseData );
						$(result).addClass('active');
						resizeStatic('#static_content');
					}
				});
//			}
}


/**
 *	hide static content, called by flash
 *	@method hideStaticContent
 */
function hideStaticContent() {
	$('#static_content').hide();
}
/**
 *	Functions called when flash wanna show static page
 *	For every static page there is specific function, url on section is taken from asset properties by specific section name.
 */
function showCalendar() {

		var content = '/sk/live_stavky/kalendar/index.html';
		AJAXoverlay( content );
}
function showResults(){
		var content = '/sk/live_stavky/vysledky/index.html';
		AJAXoverlay( content );
}
function showExplanation(){
		var content = '/sk/live_stavky/vysvetlenie_specifickych_stavkovych/index.html';
		AJAXoverlay( content );
}
function showContactForm(){
		var content = '/sk/live_stavky/kontaktny_formular/index.html';
		AJAXoverlay( content );

}
function showAffilates(){
		var content = '/sk/live_stavky/zoznam_pobociek/index.html';
		AJAXoverlay( content );
}
function showHelp(){
		var content = '/sk/live_stavky/informacie/index.html';
		AJAXoverlay( content );
}


/**
 *	ajax contact form submit script
 *	@method contact_send
 *	@param {HtmlElement} form Returns $this of form to submit
 */
function contact_send(form){
		$(form).ajaxStart(function(){ $('#loading_overlay').show(); });
		$(form).ajaxStop(function(){ $('#loading_overlay').hide(); });
		$(form).ajaxSubmit({
			target: '#loading_section',
			success: function(){
					resizeStatic( '#static_content' )
				}
		});
		return false;
}

/**
 *	ajax update user profile
 *	@method updateUserProfile
 */
function updateUserProfile() {
	var update = $('#live_login'),
		loading = document.createElement("div");
		loading.id = "loading",
		url = $('#primaryUrl').attr('value') + '?action=login&_ajax=1';
		
		$( update ).html( loading );
		
		$.ajax({
			type: "GET",
			dataType: "html",
			url: url,
			async: false,
			success: function( responseData ) {
					$(update).html( responseData );
				}
		});
}

/**
 *	Ajax login
 *	@method live_logon
 *	@param {HtmlElement} form Returns $this of login form
 */
function live_logon(form){
	$('#loading').show();

	$(form).ajaxSubmit({
		success : function( responseData ) {
			var container = $('#live_login');

			if ( container ) {
				$('#loading').hide();
				$('#live_login').html( responseData );
			}
			activate_user_menu();
		}
	});
}
/**
 *	Ajax logout
 *	@method live_logout
 *	@param {string} url url for ajax logout
 */
function live_logout( url ){
	$('#loading').show();

		$.ajax({
			type: "GET",
			dataType: "html",
			url: url,
			async: false,
			success: function( responseData ){
					$('#loading').hide();
					$('#live_login').html( responseData );
				}
		});
}
/**
 *	activating user manu for toggling
 *	@method activate_user_menu
 */
function activate_user_menu() {
	var user_link = $('#rollout');
	if ( user_link ) {
		$(user_link).click(function() {
			$('#user_nav').toggleClass( 'opened' );

			return false;
		});
	}
}
/**
 *	toggle/collapse user profile information in header
 *	@method toggleProfile
 */
function toggleProfile( target ){
	var profile = document.getElementById( 'user_logged' ),
		profile_cookie = $.cookie("profile_expand"),
		d = new Date();

	if( profile_cookie ){
		$('#user_logged').addClass('hiderows');
		$(target).removeClass('collapse');
		//$.cookie("profile_expand", null);
		d.setFullYear( d.getFullYear() - 1 );
	} else {
		$('#user_logged').removeClass( 'hiderows');
		$(target).addClass( 'collapse');
		d.setFullYear( d.getFullYear() + 1 );
	}

	$.cookie( "profile_expand", 'true', {
		path		:'/',
		expires		: d
	} );
}
$(document).ready(function() {
	// hide container for static content on DOM.ready
	$('#static_content').hide();
	activate_user_menu();
	$('#toggle_profile').click(function(event){ 
		event.stopPropagation();
		var $target = $(event.target);

		toggleProfile( $target );
	});
});
