jQuery(function() {
	
	$('.browserIE6 #site').remove();
	$('.browserIE6 #sitebg').remove();
	
	
	$('table').attr('cellspacing', 0);
	
	$('input.hintValue').each(function(i) {
		$(this).data('hint', $(this).attr('value'));
	});
	
	$('input.hintValue').focus(function() {
		var hint = $(this).data('hint');
		var value = $(this).attr('value');
		if (value == hint) {
			$(this).attr('value', '');
		}
	});
	$('input.hintValue').blur(function() {
		var hint = $(this).data('hint');
		var value = $(this).attr('value');
		if (value == '') {
			$(this).attr('value', hint);
		}
	});
	
	$(".dropDownMenu > li").mouseenter(function () {
		$(this).find("ul.sub").fadeIn(100);
	});
	$(".dropDownMenu > li").mouseleave(function () {
		$(this).find("ul.sub").fadeOut(100);
	});
	

	
//*******************************************************************//
//								TABS
//*******************************************************************//

	$('.tabcontent').hide();
	
	if(window.location.hash!=''){
		var target = window.location.hash + '-tab';
		$(target).fadeIn(200);
		$(target).addClass('visible');
		
		$('.tabnav a').each(function(){
			if( $(this).attr('href') == window.location.hash ) {
				$(this).addClass('active');
				showNextTab( $(this).parents('.tabnav').first() );
			}
		});
		//window.location.hash='';
	}
	
	/*
	$('.changeTabLink').click(function(){
		var target = $(this).attr('href');
		changeTab(target);
		//return false;
	});*/
	
	$('.tabnav').each(function(){
		if($(this).find('a.active').html() == null) {
			var firstTab = $(this).find('a').first();
			firstTab.addClass('active');
			showNextTab( $(this) );
		}
	})
	
	
	$('.tabnav a').click(function(){
		if( ! $(this).hasClass('active')) {
			//var target = $(this).attr('href');
			changeTab($(this));
			return false;
		} else {
			return false;
		}
	});
	
	
	function changeTab (next) {
		var tabnav = next.parents('.tabnav').first();
		
		tabnav.find('a.active').removeClass('active');
		next.addClass('active');
		/*
		$('.tabframe a').each(function(){
			if( $(this).attr('href') == id ) {
				$(this).addClass('active');
			}
		});*/
		
		hidePrevTab(tabnav);
	};
	function hidePrevTab(tabnav) {
		tabnav.next('.tabframe').find('.tabcontent.visible').first().fadeOut(200, function(){
			$(this).removeClass('visible');
			showNextTab(tabnav);
		});
	};
	function showNextTab(tabnav) {
		var target = tabnav.find('a.active').attr('href') + '-tab';
		$(target).fadeIn(200, function(){
			$(target).addClass('visible');
		});
	};
	
//*******************************************************************//
//						event selector menu
//*******************************************************************//
	/*	
	$('#eventSelector > li').mouseenter(function(){
		if(!$(this).hasClass('active')) {
			$('#eventSelector').data('using' , true);
			$('#eventSelector > li.active').find('ul.sub').fadeOut(150);
			$(this).find('ul.sub').fadeIn(150);
		}
	});
	$('#eventSelector > li').mouseleave(function(){
		if(!$(this).hasClass('active')) {
			$('#eventSelector').data('using' , false);
			$(this).find('ul.sub').fadeOut(150, function(){
				if(!$('#eventSelector').data('using')) $('#eventSelector > li.active').find('ul.sub').fadeIn(150);
			});
		}
	});
	*/

//*******************************************************************//
//							banners
//*******************************************************************//	
	$('.banners .banner > * ').fadeTo(100, 0.5);
	$('.banners .banner').mouseenter(function(){
		$(this).children().first().fadeTo(100, 1);
	});
	$('.banners .banner').mouseleave(function(){
		$(this).children().first().fadeTo(100, 0.5);
	});
	
	
//*******************************************************************//
//							input fix
//*******************************************************************//		
	$('.fixInputZ').each(function() {
		$(this).css('zIndex', 10);
	});
	$('.fixInputZ').focus(function() {
		$(this).css('zIndex', 1000);
	});
	
	$('.fixInputZ').blur(function() {
		$(this).css('zIndex', 10);
	});

//*******************************************************************//
//							COMBOBOX
//*******************************************************************//
	$('.combobox').click(function() {
		var combobox = $(this);
		if (combobox.hasClass('opened')) {
			combobox.find('.options').fadeOut(100, function(){
				combobox.removeClass('opened');
				combobox.blur();
			});
		} else {
			combobox.focus();
			combobox.find('.options').fadeIn(100, function(){
				combobox.addClass('opened');
			});
		}
	});
	
	$('.combobox').mouseleave(function() {
		var combobox = $(this);
		if (combobox.hasClass('opened')) {
			combobox.find('.options').fadeOut(100, function(){
				combobox.removeClass('opened');
				combobox.blur();
			});
		}
	});
	$('.combobox ul.options li').click(function() {
		var label = $(this).html();
		var value = $(this).data('value');
		
		var combobox = $(this).parents('.combobox');
		
		combobox.find('.label').html(label);
		combobox.attr('title', label);
		combobox.find('.value').val(value);
		combobox.addClass('selected');
	});
	
	
	if ($('.combobox ul.options').find('li[title="' + $('.combobox .value').attr('value') + '"]').html()) {
		$('.combobox .label').html($('.combobox ul.options').find('li[title="' + $('.combobox .value').attr('value') + '"]').html());
	}
	
	$('.combobox ul.options li').each(function() {
		$(this).data('value', $(this).attr('title')).removeAttr('title');
	});

//*******************************************************************//
//							STACKBOX
//*******************************************************************//
	$('.stackbox .options input').each(function() {
		var stackbox = $(this).parents('.stackbox').first();
		var stack = stackbox.find('.stack');
		$(this).identify('category');
		$(this).siblings('label').attr('for', $(this).attr('id'));
		
		if($(this).attr('checked')) {
			//$(this).parents('li').first().addClass('checked');//
			stack.append($(this).parents('li').first());
		}
	});
	
	$('.stackbox .add .button').click(function() {
		var stackbox = $(this).parents('.stackbox');
		if (stackbox.hasClass('opened')) {
			stackbox.find('.options').fadeOut(100, function(){
				stackbox.removeClass('opened');
				stackbox.blur();
			});
		} else {
			stackbox.focus();
			stackbox.find('.options').fadeIn(100, function(){
				stackbox.addClass('opened');
			});
		}
	});
	$('.stackbox .add').mouseleave(function() {
		var stackbox = $(this).parents('.stackbox');;
		if (stackbox.hasClass('opened')) {
			stackbox.find('.options').fadeOut(100, function(){
				stackbox.removeClass('opened');
				stackbox.blur();
			});
		}
	});
	
	$('.stackbox .optlist li').click(function() {
		var stackbox = $(this).parents('.stackbox').first();
		var stack = stackbox.find('.stack').first();
		var opts = stackbox.find('.options').first();
		
		if ($(this).find('input').attr('checked')) {
			$(this).find('input').attr('checked', false);
			opts.append($(this));
		}
		else {
			$(this).find('input').attr('checked', true);
			stack.append($(this));
		}
		return false;
	});
	
	$('.expandlist > li > .switch').click(function(){
		var container = $(this).parents('li').first();
		var content = container.find('.cont');
		if(container.hasClass('opened')){
			content.slideUp(200);
			container.removeClass('opened');
		} else {
			content.slideDown(200);
			container.addClass('opened');
		}
	});
	
//*******************************************************************//
//								BG IMAGES
//*******************************************************************//	
	
	bgImages = $('#bgFader').children();
	$('#bgFader').empty();
	bgImages = $.shuffle(bgImages);
	bgFaderStart();

//*******************************************************************//
//								SEAT CHOOSER
//*******************************************************************//
	$('.seatChooser').each(function(){
		seatChooser($(this));
	});
});


var s_left = 'left';
var s_right = 'right';
var s_row = 'row';
var s_seat = 'seat';
var s_table = 'table';
var s_dancefloor = 'dancefloor';
var s_section = 'section';
var s_reserved = 'reserved';
var ticketForm = true;

function seatChooser(chooser){
	if(chooser.data('trans') != null) {
		
		s_left = chooser.data('trans').left;
		s_right = chooser.data('trans').right;
		s_row = chooser.data('trans').row;
		s_seat = chooser.data('trans').seat;
		s_table = chooser.data('trans').table;
		s_dancefloor = chooser.data('trans').dancefloor;
		s_section = chooser.data('trans').section;
		s_reserved = chooser.data('trans').reserved;
	}
	chooser.find('.section').each(function(){
		var section = $(this);
		var seats = section.find('.seats');
		var tables = section.find('.tables');

		section.addClass(section.data('section'));
		
		seats.data('day', chooser.attr('id'));
		tables.data('day', chooser.attr('id'));
		seats.data('section', section.data('section'));
		tables.data('section', section.data('section'));

		drawSeats(seats);
		drawTables(tables);
	});
	
	chooser.find('.vertical .row:not(.r7) .seat.s13').addClass('space').removeAttr('title');
	chooser.find('.vertical .row.r7 .seat').addClass('disabled').removeAttr('title');
	
	chooser.find('.horizontal .seat.s18').addClass('space').removeAttr('title');
	chooser.find('.horizontal .row:not(.r3) .seat.s17').addClass('space').removeAttr('title');
	
	for ( var r = 0; r < chooser.data('reserved').length; r++){
		var place = $('#'+chooser.data('reserved')[r]);
		place.addClass('reserved').attr('title', s_reserved.toUpperCase() + '! ' + place.attr('title'));
	}
	
	initFreePlaces(chooser);
	
	$('#ticketForm .seatMap').hide();
	$('#ticketForm .selectedPlaces').ready(function(){
		if(ticketForm) initTicketForm();
	});
	
}
function initTicketForm(){
	$('#ticketForm .seatPlaces').each(function(){
		$(this).data('emptyString', $(this).find('.list').html());
		$(this).data('selected', 0);
	});
	ticketForm = false;
}
function initFreePlaces(chooser){
	chooser.find('.place:not(.reserved, .disabled, .space)').each(function(){
		var place = $(this);
		place.addClass('active');
		place.append('<input type="checkbox" name="'+place.attr('id')+'"/>');
	});
	chooser.find('.place.active input').bind('change', function(){
		var place = $(this).parent('.place');
		var id = place.attr('id');
		var day = id.substr(0,2);
		var type = place.data('type');
		var dayFrame = $('#ticketForm .'+day+'.seatPlaces');
		var list = dayFrame.find('.list');

		if($(this).attr('checked')) {
			if(dayFrame.data('selected') == 0) list.html('');
			list.append('<span class="'+id+' '+type+' place" data-id="'+id+'">'+place.attr('title')+'</span>');
			dayFrame.data('selected', dayFrame.data('selected')+1);
			place.addClass('checked');
		}
		else {
			list.find('.'+id).remove();
			if(dayFrame.data('selected') > 0) dayFrame.data('selected', dayFrame.data('selected')-1);
			if(dayFrame.data('selected') == 0) list.html(dayFrame.data('emptyString'));
			place.removeClass('checked');
		}
		updatePlaceInput(dayFrame.children('input.'+type));
		update_foglal_ar(id);
	});
	
}
function updatePlaceInput(input){
	var value = '';
	var dayFrame = input.parents('.seatPlaces').first();
	var list = dayFrame.find('.list');
	var type = input.data('type');
	list.find('.'+type+'.place').each(function(){
		value += $(this).data('id') + ',';
	});
	input.val(value);
}
function drawSeats(seats){
	var row = seats.data('row');
	var col = seats.data('col');
	var mid = Math.ceil(col/2);
	var even = (mid == (col/2)) ? 1 : 0;
	
	var line = '';
	var seat;
	var side;
	var firstHalf = true;
	
	for( var r = 1; r <= row; r++) {
		line = '<div class="row r'+r+' clearfix" data-row="'+r+'">';
		seat = 1;
		
		if (seats.data('section') == 'B') {
			s_side = s_right;
			side = 'right';
		}
		else {
			s_side = s_left;
			side = 'left';
		}
		for(var c = 1; c <= col; c++) {
			if (c == mid && even == 0) {
				if(seats.data('section') == 'B') {
					if (firstHalf) {			
						side = (side == 'left') ? 'right' : 'left';
						s_side = (s_side == s_left) ? s_right : s_left;
						firstHalf = false;
					}
				}
			}
			line += '<label id="'+seats.data('day')+seats.data('section')+'r'+r+'s'+side.charAt(0).toUpperCase()+seat+'" class="seat place '+side+' s'+seat+'" title="'+seats.data('section')+' '+s_section+', '+r+'. '+s_row+', '+s_side.toUpperCase()+' '+seat+'. '+s_seat+'" data-type="seats" data-side="'+side+'" data-seat='+seat+'>&nbsp</label>';
			if( c < mid ) seat++;
			else {
				seat--;
				if (seat==(mid-even)) {
					seat = mid;
					even = -1;
				}
				if (firstHalf) {
					side = (side == 'left') ? 'right' : 'left';
					s_side = (s_side == s_left) ? s_right : s_left;
					firstHalf = false;
				}
			}
		}
		line += '</div>';
		firstHalf = true;
		even = (even == -1) ? 1 : 0;
		if (seats.data('section') == 'A')
			seats.prepend(line);
		else
			seats.append(line);
	}
}
function drawTables(tables){
	var tn = tables.data('tables');
	var sn = 5;
	var table = '';
	for ( var t = 1; t <= tn; t++) {
		table = '<div class="table t'+t+'" data-table="'+t+'">';
		
		for (var s = 1; s <= sn; s++) {
			table += '<label id="'+tables.data('day')+tables.data('section')+'t'+t+'s'+s+'" class="seat s'+s+' place" title="'+tables.data('section')+' '+s_section+', '+t+'. '+s_table+', '+s+'. '+s_seat+'" data-type="tables" data-seat"'+s+'"></label>';
		}
		
		table += '</div>';
		if (tables.data('section') == 'B')
			tables.prepend(table);
		else
			tables.append(table);
	}
}


//*******************************************************************//
//								BG IMAGES
//*******************************************************************/
var nextIndex = 0,
	lastIndex,
	bgImageSources = new Array(),
	bgImages = new Array(),
	bgTimer;


function bgFaderStart() {
	
	$('#bgFader').append(bgImages[nextIndex]);
	$(bgImages[nextIndex]).addClass('resize').load(function(){
		resizeBg();
	});
	$(bgImages[nextIndex]).fadeTo(1000, 0.5);
	$('#bgFader').fadeIn(1000);
	
	$(window).resize(function(){
		resizeBg();
	});
	resizeBg();
	setTimeout("fadeBg()", 30000);
	
};
function fadeBg() {
	lastIndex = nextIndex++;
	if(nextIndex == bgImages.length) nextIndex = 0;
	if($('#bgFader').index(bgImages[nextIndex]) <= 0){
		$('#bgFader').append(bgImages[nextIndex]);
	}
	$(bgImages[lastIndex]).fadeOut(1000, function(){$(this).removeClass('resize')});
	$(bgImages[nextIndex]).addClass('resize');
	$(bgImages[nextIndex]).fadeTo(1000, 0.5);
	
	resizeBg();
	
	setTimeout("fadeBg()", 30000);
}
function resizeBg() {
	$('#bgFader .resize').each(function() {
		
		var windowRatio = $(window).width() / $(window).height();
		var imageRatio = $(this).width() / $(this).height();
		
		if (windowRatio > imageRatio) {
			var adjust = ( $(window).height() < ($(this).height()*0.75) ) ? '-30%' : '0px';
			$(this).css({
				'width' : '100%',
				'height' : 'auto',
				'top' : adjust
			});
		} else {
			var adjust = ( $(window).width() < ($(this).width()*0.40) ) ? '-60%' : '0px';
			$(this).css({
				'width' : 'auto',
				'height' : '100%',
				'left' : adjust
			});
		}
	});
}

