$(document).ready(function() {
	UpdateHome();
	if ($(".promo_session").size() > 1) {
		$("#events_scroll").scrollable({size:1, keyboard: false, clickable: false}).circular().navigator().autoscroll({steps: 1, interval: 7000});
	}
});

function refreshAds(el, width, height) {
	var currentTime = new Date();
	$(el).html('<iframe id="googlead" class="googlead" name="googlead" href="" src="/ads/?s=' + currentTime.getTime() + '&w='+width+'&h='+height+'" width="'+width+'" height="'+height+'" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe>' ) ;
}

function DisplayEvent(key) {
	var imgTesting = new Image();
	function CreateDelegate(contextObject, delegateMethod) {
		return function() {
			return delegateMethod.apply(contextObject, arguments);
		};
	}
	function imgTesting_onload() {
		width = this.width;
		height = this.height;

		$.ajax({
			url: '/',
			data: 'home/events/id/'+key+'/xhr',
			success: function(msg){
				if (msg.length > 0) {
					$('#dialog_box').dialog('option', 'buttons', { });
					$('#dialog_box').html(msg);
					$('#dialog_box').dialog('option', 'bgiframe', true);
					$('#dialog_box').dialog('option', 'width', width + 22);
					$('#dialog_box').dialog('option', 'height', 'auto');
					$('#dialog_box').dialog('open');
					$('#dialog_box').dialog('option', 'height', height + $('#event_box').height() + 102);
					$('#dialog_box').dialog('option', 'title', $('#event_box').attr('title'));
					$(document).click(function() { $('#dialog_box').dialog('close'); $(document).unbind("click"); });
					$('#dialog_box').click(function(e) {e.stopPropagation();});
				}
			}
		});
		$('.items').unblock();
	}
	$('.items').block({ message: null, overlayCSS: { backgroundColor: '#FFF' } });
	imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
	imgTesting.src = '/image/event/flyer/'+key;
}

function DisplayContest(key) {
	$('#'+key).block({ message: null, overlayCSS: { backgroundColor: '#FFF' } });
	$.ajax({
		url: '/',
		data: 'home/contests/id/'+key+'/xhr',
		success: function(msg){
			$('#'+key).unblock();
			if (msg.length > 0) {
				$('#'+key).html(msg);
			}
		}
	});
}

function Play(key) {
	var win = window.open("/player/audio/playlist/"+key+"/random", 'audio_player_win', 'height=390, width=440, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
	if (!win) {
		 alert("A popup blocker has been detected, please turn it off for listen to music !");
	}
}

function SubmitPool() {
	DeasactiveForm('#poll_form');
	$.ajax({
		type: 'POST',
		url: $('#poll_form').attr('action'),
		data: $('#poll_form').serialize(),
		success: function(msg) {
			ActiveForm('#poll_form');
			if (msg.indexOf('error:') > -1) {
				DisplayError($('#poll_form_error'), msg);
			} else {
				$('#poll_div').html(msg);
				$('.poll_bar').hide().animate({width: "toggle"}, 1500);
			}
		}
	});
}

function UpdateHome(module) {
	$('.content_control > a').removeClass("selected");
	if (typeof(module) != 'undefined') {
		if ($('#home_link_'+module).length) {
			$('#home_link_'+module).addClass("selected");
		} else {
			$('#home_link_all').addClass("selected");
		}
	} else {
		var result = UrlParse(window.location.href, 1);
		var ActivPage = '#home_link_'+result;
		if ($(ActivPage).length) {
			$(ActivPage).addClass("selected");
		} else {
			$('#home_link_all').addClass("selected");
		}
	}
}

function ChangeTo(module) {
	$('#content').block({ message: null, overlayCSS: { backgroundColor: '#FFF' }  });
	$.ajax({
		url: '/',
		data: 'home/1/xhr',
		success: function(msg){
			$('#content').unblock();
			$('#content').html(msg);
			UpdateHome(module);
		}
	});
}

function ShowMore(start) {
	$('#content').block({ message: null, overlayCSS: { backgroundColor: '#FFF' }  });
	$.ajax({
		url: '/',
		data: 'home/'+start+'/xhr',
		success: function(msg){
			$('#content').unblock();
			$('#show_more_botton').remove();
			if ($('#buttom_ads').length > 0) {
				$('#buttom_ads').remove();
			}
			$('#content').append(msg);
		}
	});
}

function RefreshSelecta() {
	$('#random_selecta').block({ message: null, overlayCSS: { backgroundColor: '#FFF' }  });
	$.ajax({
		url: '/',
		data: 'home/selecta/xhr',
		success: function(msg){
			$('#random_selecta').html(msg);
		}
	});
}

function ShowEvent(date) {
	if (typeof(date) == "undefined" || date === null) {
		type = 'wrong';
	}
	$('#menu_calendar').block({ message: null, overlayCSS: { backgroundColor: '#FFF' } });
	$.ajax({
		url: '/',
		data: 'home/events/for/'+date+'/xhr',
		success: function(msg){
			$('#menu_calendar').html(msg);
			UpdateInterface();
		}
	});
}
