var concert_list = new Array();
var selected_concert;
var vip_list = new Array();
var selected_vip;
var load_finished = false;

function initAnimations() {
	getConcerts();
	getVIPs();
}

function getConcerts() {
	$.ajax({
		type: "GET",
		url: "./_php/getConcerts.php",
		dataType: "json",
		success: (function(data) {
			concert_list = data;
			if (load_finished) {
				changeImages();
				window.setInterval(function() {changeImages(); }, 4000);
			}
			else {
				load_finished = true;
			}
			//console.log('Nr. concerte: ' + concert_list.length);
		})
	});
}

function getVIPs() {
	$.ajax({
		type: "GET",
		url: "./_php/getVIP.php",
		dataType: "json",
		success: (function(data) {
			vip_list = data;
			if (load_finished) {
				changeImages();
				window.setInterval(function() {changeImages(); }, 5000);
			}
			else {
				load_finished = true;
			}
			//console.log('Nr. VIP-uri: ' + vip_list.length);
		})
	});
}

function changeImages() {
	if (concert_list.length) {
		$('#concert').css('display', 'block');
		var current_concert = $('#concert a.showing');
		if (current_concert.length) {
			current_concert.removeClass('showing');
		}
		var next_id = Math.floor(concert_list.length * Math.random());
		if (selected_concert != null) {
			concert_list.push(selected_concert);
		}
		selected_concert = (concert_list[next_id] != null)?concert_list[next_id]:null;
		concert_list.splice(next_id, 1);
		var next_concert = jQuery('<a />', {
			href: selected_concert.link,
			title: selected_concert.title,
			css: {
				opacity: 0,
				filter: 'alpha(opacity=0)',
				position: 'absolute',
				left: 0,
				top: 0,
				'z-index': 25
			}
		}).appendTo('#concert');
		jQuery('<img />', {
			src: './images/concerte/img/' + selected_concert.img,
			alt: selected_concert.title,
			css: {
				width: '180px',
				height: 'auto'
			}
		}).appendTo(next_concert);
		//console.log('Concert: ' + selected_concert.title + ' ' + selected_concert.img);
		next_concert.addClass('showing');
		next_concert.animate({opacity: 1.0}, 1500);
		if (current_concert.length) {
			current_concert.animate({opacity: 0.0}, 1500, function() {
				next_concert.css('z-index', 50);
				current_concert.remove();
			});
		}
	}
	if (vip_list.length) {
		var current_vip = $('#VIP div.showing');
		if (current_vip.length) {
			current_vip.removeClass('showing');
		}
		var next_id = Math.floor(vip_list.length * Math.random());
		if (selected_vip != null) {
			vip_list.push(selected_vip);
		}
		selected_vip = (vip_list[next_id] != null)?vip_list[next_id]:null;
		vip_list.splice(next_id, 1);
		var next_vip = jQuery('<div />', {
			css: {
				backgroundColor: '#000',
				width: '200px',
				height: '150px;',
				position: 'absolute',
				left: 0,
				top: 0,
				'z-index': 25
			}
		}).appendTo('#VIP');
		jQuery('<img />', {
			src: './images/vip/' + selected_vip.img,
			alt: selected_vip.titlu,
			css: {
				height: '150px',
				width: 'auto',
				display: 'block',
				marginLeft: 'auto',
				marginRight: 'auto'
			}
		}).appendTo(next_vip);
		jQuery('<div />', {
			text: selected_vip.titlu,
			css: {
				width: '100%',
				position: 'absolute',
				bottom: '-20px',
				height: '20px',
				'line-height': '20px',
				textAlign: 'center',
				backgroundColor: '#000',
				color: '#fff',
				opacity: 0,
				filter: 'alpha(opacity=0)',
				'z-index': 75
			}
		}).appendTo(next_vip);
		//console.log('VIP: ' + selected_vip.titlu + ' ' + selected_vip.img);
		next_vip.addClass('showing');
		next_vip.animate({opacity: 1.0}, 1000, function() {
			next_vip.css('z-index', 50);
			next_vip.find('div').animate({bottom: 0, opacity: 0.7}, 500);
		});
		if (current_vip.length) {
			current_vip.animate({opacity: 0.0}, 1000, function() {
				current_vip.remove();
			});
		}
	}
}

function showSplashBanner() {
	if (($.cookies.get('splash') == null) || ($.cookies.get('splash') != 'yes')) {
		$.cookies.set('splash', 'yes');
		$.ajax({
			type: 'GET',
			url: './_php/getSplash.php',
			dataType: 'json',
			success: (function(data) {
				if (data.error === false) {
					var splash_container = jQuery('<div />', {
						css: {
							id: 'splashContainer',
							position: 'fixed',
							top: 0,
							left: 0,
							width: '100%',
							height: '100%',
							'background-color': '#000',
							opacity: 0.7,
							filter: 'alpha(opacity=70)',
							'z-index': 1000
						}
					}).appendTo('body');
					var splash_link_container = jQuery('<div />', {
						css: {
							position: 'fixed',
							top: '50%',
							left: '50%',
							display: 'block',
							'z-index': 1100
						}
					}).appendTo('body');
					var splash_link = jQuery('<a />', {
						href: data.splashLink,
						title: data.splashTitle,
						target: '_blank',
						click: function() {
							$(splash_link_container).hide();
							$(splash_container).hide();
							$(splash_link_container).remove();
							$(splash_container).remove();
						},
						/*css: {
							position: 'fixed',
							top: '50%',
							left: '50%',
							display: 'block',
							'z-index': 1100
						}*/
					}).appendTo(splash_link_container);
					var splash_img = jQuery('<img />', {
						alt: data.splashTitle,
						css: {
							border: '1px solid #fff',
						}
					}).appendTo(splash_link);
					$(splash_img).hide();
					$(splash_img)
						.load(function() {
							$(splash_link_container).css({
								width: $(this).width(),
								height: $(this).height(),
								marginTop: -($(this).height() / 2),
								marginLeft: -($(this).width() / 2)
							});
							$(this).fadeIn();
						})
						.attr('src', data.splashImg);
					var link_pos = $(splash_link).position();
					var close_button = jQuery('<a />', {
						title: 'Inchide',
						text: 'Inchide',
						css: {
							display: 'block',
							//'background-color': '#fff',
							//color: '#000',
							'text-align': 'right',
							cursor: 'pointer'
						}
					}).appendTo(splash_link_container);
					$(close_button).click(function() {
						$(splash_link_container).hide();
						$(splash_container).hide();
						$(splash_link_container).remove();
						$(splash_container).remove();
					});
				}
			})
		});
	}
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9019840-8']);
_gaq.push(['_trackPageview']);

$(document).ready(function() {
	var ga = document.createElement('script');
	ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(ga, s);
	$('input[name="query"]').focus(function() {
		if (this.val() == 'search...') {
			this.val('');
		}
	});
	$('input[name="query"]').blur(function() {
		if (this.val() == '') {
			this.val('search...');
		}
	});
	//$('input[name="submit"]').hover(function() {this.css({backgroundPosition, '0 -28px'});}, function() {this.css(backgroundPosition, '0 0');});
	initAnimations();
	showSplashBanner();
	$('#menu ul li').hover(function(){
		$(this).find('span').stop().animate({top: '0'}, 250);
	}, function(){
		$(this).find('span').stop().animate({top: '29px'}, 250);
	});
});
