App = {
	emptyFunction : function() {},
	pageName : '',
	X1_ad_params : {},

	initialise : function() 
	{
		App.fixLayoutWidths();
		this.cine = (window.location.host.indexOf('oradea') == -1) ? 11 : 12;
		
		var username = $.cookie('username');
		if (username != null)
		{
			$('#loginForm').hide();
			$('#loggedIn').show();
			$('#logged_user').html(username.replace(/\+/, "&nbsp;"));
		}
		
		/* X1 Banner parameters */

		var category = document.location.pathname.replace(RegExp("(.+/)[^/]+"), "$1");
		
		if ('undefined' != category && '/' == category)
		{
			this.X1_ad_params = "pub=632203491294404&site=hmultiplex&section=homepage&zone=rectangle_video&size=0x0";
		}
		else
		{
			this.X1_ad_params = "pub=632203491294404&site=hmultiplex&section=site&zone=rectangle_video&size=0x0";
		}


		this.initFilters();
		
		if (typeof App.selectedMenuButton != 'undefined')
		{
			$('#' + App.selectedMenuButton).addClass('hover');
		}
		
		if (typeof this.initors[this.pageName] == 'function') 
		{
			App.initors[this.pageName]();
		} 
		
		$('#filtersGo').click(function() 
		{
			var url = App.getFiltersUrl();
			window.location.href = url;			
		});	

		$('#submit_nl').click(function()
		{
			var email = $('#email_nl');
			var email_val = email.val().replace('<','').replace('>','');
			email.val(email_val);
			return true;
		});		
		
		$('#footerTabs').nextTabs();
		
		$('.stats').nextRating({
			captchaErrorMessage : 'Numar introdus gresit!',
			type : 'image'
		});		
		
		$('body').nextComments();

		var selectCinema = $('#selectCinema');
		
		selectCinema.bind('change', function() 
		{
			var url = (this.value == 'bucuresti') ? 'http://hmultiplex.mediaproi.ro' : 'http://oradea.hmultiplex.mediaproi.ro';
			window.location.href = url;
			return true;
		});
		
		if (App.cine == 12)
		{
			selectCinema.attr('selectedIndex', 1);
		}
		else
		{
			selectCinema.attr('selectedIndex', 0);
		}
		
		var hpTimer = $('#hpTimer');
		App.setTimer(hpTimer);	
		setInterval(function() {
			App.setTimer(hpTimer);			
		}, 60000);
		
		$('#srcQ').click(function() {
			var _this = $(this);
			var val = _this.val()
			if (val == 'cauta') 
			{
				_this.val(' ');
			}
		});

		if (window.location.hash == '#badlogin')
		{
			setTimeout(function() {
				alert('Autentificare nereusita!' + "\n" + 'Va rugam introduceti datele de logare corecte');
				
			}, 1000);	
		}
	},
	
	fixLayoutWidths : function()
	{
		var tbl = $('#tableContent');
		var tdCenter = $('#tdCenter');
		var newWidth = ~~((tbl.width() - tdCenter.width()) / 2);
		$('#tdLeft, #tdRight').css('width', newWidth);		
	},
	
	initSpecialBanner : function()
	{
		
	},

	initFilters: function()
	{	
		bindOnChange = typeof arguments[0] == 'undefined' ? true : false;

		var week = $('#filtersWeek');
		
		if (week.length == 0)
		{
			return false;
		}
		
		weekVal = week.val();
		var select = $('#filtersDay');
		var optDays = $('option', select);
		
		if (typeof App.optDaysBak == 'undefined')
		{
			App.optDaysBak = optDays.clone();	
		}
		
		if (weekVal != 'saptamana-curenta')
		{
			//optDays.attr('disabled', false);
			select.empty().append(App.optDaysBak);
			selectedIdx = select.attr('selectedIndex');
		
			if (selectedIdx < 1)
			{			
				optDays = $('option', select);
				optDays.eq(0).attr('selected', true);		
			}
			return false;
		}
		
		var thedate = new Date();
		var day = thedate.getDay();
		
		
		for (i = 1; i < day; i++)
		{
			optDays.eq(i).remove();
		}
		selectedIdx = select.attr('selectedIndex');

		if (selectedIdx < 1)
		{
			//optDays.eq(day).attr('selected', true);		
		}
		if (bindOnChange == true) 
		{
			week.bind('change', function(){
				App.initFilters(false);
			});
		}
	},
	
	setTimer : function(t)
	{
		var currentTime = new Date();
		var hour = currentTime.getHours();
		var minutes = currentTime.getMinutes();
			
		if (minutes < 10){
			minutes = "0" + minutes;
		}		
			
		if (hour < 10){
			hour = "0" + hour;
		}			
			
		var html = hour + ':' + minutes;
		t.html(html);		
	},
		
	getFiltersUrl : function()
	{
		if (App.pageName != 'arhiva')
		{
			var url = '/program/';		
			var week  = $('#filtersWeek').val();
			var day   = $('#filtersDay').val();			
			var genre = $('#filtersGenre').val();
			
			url += week + '/';
			if (day != 0)
			{
				url += day + '/';
			}
	
			if (genre != 0)
			{
				url += genre + '/';
			}	
		}
		else 
		{
			var url = '/filme/';	
			var order  = $('#filtersOrder').val();
			var letter   = $('#filtersLetter').val();			
			
			if (order != 'cronologic')
			{
				url += order + '/';
			}
			if (letter != 0)
			{
				url += letter + '/';
			}		
			
			
		}
		
		return url;
		
	},
	
	rand : function( min, max ) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}	
}

App.initors = {
	'homepage' : function() {
	

		var master = $('ul', $('#movieRowMaster'));
		var containers = $('.movieRowContainer', master);
		
		if (containers.length > 1)
		{
			$('#btnNext').removeClass('disabled');
		}
		else 
		{
			$('#btnNext, #btnPrev').css('cursor', 'auto');
		}
		var rnd = 0;
		var ok = true;
		var r = 0;
		var cnt = 0;
		if (containers.length > 1) 
		{
			while (ok && cnt < 10) {
				rnd = App.rand(0, (containers.length - 1));
				r = $('.movie', containers.eq(rnd)).length;
				if (r == 6) {
					ok = false;
				}
				cnt++;
			}
			
			if (cnt == 10) {
				rnd = 0;
			}
		}
		master.prepend(containers.eq(rnd));
		containers = $('.movieRowContainer', master);

						
		var firstContainer = containers.eq(0)		
		var movieRows = $('.movieRow', firstContainer);
	
		ok = true;
		var usedRands = [];
		rnd = null;
		cnt = 0;
		for (i = 0; i < 3; i++)
		{
			
			while (ok && cnt < 10)
			{
				rnd = App.rand(0, movieRows.length );		
				
				if (typeof usedRands[rnd] == 'undefined')
				{
					ok = false;
					usedRands[rnd] = 1;
				}	
				cnt++;
			}			
			if ($('.movie', movieRows.eq(rnd)).length == 2)
			{
				firstContainer.prepend(movieRows.eq(rnd));				
			}
			ok = true;
		}
		
		if ($('.movie').length > 0)
		{
			$('#movieRowMaster').jCarouselLite({
				btnNext: "#btnNext",
				btnPrev: "#btnPrev",
				circular: false,
				visible: 1
			});
		}
		//containers.show();
		
		$('#mediaplayer').nextMediaplayer({
			width : 260,
			height: 220,
			hasAd : true,
			X1_ad_params : App.X1_ad_params
		});	
	
		$('#somepageMoviePlayer').nextMediaplayer({
			width : 325,
			height: 245,
			hasAd : true,
			X1_ad_params : App.X1_ad_params
		});	

		var slideNumbers = $('a', $('#slideNumbers'));		
		var slides = $('#slides');
		var players = $('.mediaplayerFlv', slides);
		
		var slideshow = slides.nextSlideshow({
			autostart : true,
			staticTime : 6000,
			callback : function(idx) 
			{				
				setTimeout(function() {
					slideNumbers.removeClass('hover');
					slideNumbers.eq(idx).addClass('hover');
				}, 700);				
			}
		});
		
		slideNumbers.click(function() {
			var idx = parseInt($(this).text());
			
			$('.mediaplayerFlv', slides).html('');			
			$('.mpThumb').show();
			//setTimeout(function() {	}, 1000);
			$.fn.nextSlideshow.jumpTo(slideshow, idx - 1);
		});
		
		$('.play').click(function() {		
			$.fn.nextSlideshow.stop(slideshow.get(0));
			var parent = $(this).parent();
			var input = $('.mediaplayerSrc', parent);
			var _source = (input.hasClass('mediaplayerTare')) ? 1 : 0;
			var url = input.val();
            var _width = 325;
			var _height = 245;
			if(_source == 1)
			{
				_width = 280;
				_height = 203;
				$("#mediaplayerFlv").css("padding-top", 35);
			}
			else
			{
				$("#mediaplayerFlv").css("padding-top",0);
			}
			$('.mpThumb', parent.parent()).hide();
 
			parent.parent().nextMediaplayer({
				width : _width,
				height: _height,
				hasAd : true,
				X1_ad_params : App.X1_ad_params,
				source : _source,
				flashvars : {
					autostart : true,
					file : url
				}				
			});			
			
		});
		
		var hideshow = $('#hideshow');
		hideshow.click(function() {
		
			$('.mediaplayerFlv', slides).html('');			
			$('.mpThumb').show();			
			
			$('#top_flash').slideToggle('slow');
			if (hideshow.hasClass('ascunde'))
			{				
				hideshow.removeClass('ascunde');
				hideshow.addClass('arata');				
			}
			else
			{			
				hideshow.removeClass('arata');
				hideshow.addClass('ascunde');				
			}			
		});

	},
	
	'film' : function()
	{
		$('#movieTabs').nextTabs();
		$('#mediaplayer').nextMediaplayer({
			hasAd : true,
			X1_ad_params : App.X1_ad_params
		});		
	},

	'stire' : function()
	{
	
		$('#mediaplayer').nextMediaplayer({
			width : 300,
			height: 275,
			hasAd : true,
			X1_ad_params : App.X1_ad_params
		});
	},
	
	'promotie' : function()
	{
	
		$('#mediaplayer').nextMediaplayer({
			width : 300,
			height: 275,
			hasAd : true,
			X1_ad_params : App.X1_ad_params
		});

	},

	'trailere' : function()
	{
		$('#carousel').jCarouselLite({
	        btnNext: ".btnNext",
	        btnPrev: ".btnPrev",
			circular: true,
			visible : 6,
			scroll : 6
	    });	
		
		var playlists = $('.trailerPlaylist');
		var titles = $('.trailerTitle');
		var mediaplayer = $('#mediaplayer').nextMediaplayer({
			width : 520,
			height: 350,
			hasAd : true,
			X1_ad_params : App.X1_ad_params,
			flashvars : {
				autostart : true
			}
		});
		
		$('.trailerMovie').click(function(){
			var _this = $(this);
			var i = $('input.key', _this).eq(0).val();
			var previewImg = $('input.previewImg', _this).eq(0).val();

			var idxFirstTrailer = mediaplayer.getItemIndex($('.mediaplayerItem:first', playlists.eq(i)).get(0));
			
			titles.hide();
			titles.eq(i).show();
			playlists.hide();
			playlists.eq(i).show();	
			
			mediaplayer.play(idxFirstTrailer, false, previewImg);
			window.location.hash = 'trailerVideo';
			
		});
		
		var tc= $('#trailersContainer');
		$('#allTrailers').click(function() {
			$('#trailersCarousel, .navBtn, .vezi_toate').hide();			
			//tc.fadeIn('slow');
			tc.show();

			window.location.hash = 'trailers';
		});
		

	},
	
	'campaign_item' : function()
	{
		$('#rating').nextRating(
		{			
			type : 'image'
		});	
		
		$('#embed').click(function()
		{
			var _message = '<div style="padding:10px;"><p style="padding:10px;font-size:12px;">Va rugam sa copiati codul din caseta de mai jos</p>';
			_message += '<textarea style="width:370px;height:190px;font-size:11px;overflow:hidden;">' + $('#embed_code').html() + '</textarea><br /><br /><a id="unblock" href="javascript:;">[ Inchide fereastra ]</a></div>';
			$.blockUI({message : _message});
			
			$('#unblock').click(function()
			{
				$.unblockUI();
			});
			
		});
	},	
	
	'campaign' : function()
	{
		$('#sort').change(function()
		{
			window.location.href  = $(this).val();
			
		});
	}
};

$(document).ready(function() 
{
	$(document).pngFix(); 
	App.initialise();	
	
});	

function rezervaBilet(id)
{
	var newWin;				
	newWin = window.open("/tickets/?siteId=" + App.cine + "&showId="+id,"CPL","width=870,height=670,scrollbars=1,menubar=no,resizable=no,top=" + ( screen.width - 770 ) / 2 + ",left=" + ( screen.height - 570 ) / 2 + ",status=no");
	if (newWin)
	{
		newWin.focus(); 
	}
}

function rezervaBiletH(id)
{
	var adSeen = $.cookie('hkad');
	var testBanner = 0;//window.location.search.indexOf('testbanner');
	
	if (adSeen == null && testBanner > -1)
	{
		var options = { path: '/', expires: 1 };
		$.cookie('hkad', 'yes', options);

		var html = '';
		//html += '<div style="font-size:20px;font-weight:bold;color:#fff;padding:2px 0 5px 0;margin-bottom:20px;text-align:center;">Cautam cel mai bun loc pentru dumneavoastra, va rugam asteptati!</div>';
		html += '<img src="/images/default/cautam.gif" alt="" />';
		html += '<iframe src="/?template=special_promo" scrolling="no" frameborder="0" style="border:none;width:550px;height:550px;overflow:hidden;margin:40px 0 0 0;padding:0"></iframe>';
	//	html += '<div style="margin:5px 0;">';
	//	html += '<object width="550" height="550"><param name="movie" value="/images/default/banners/ucl_tranzitional_550x550px.swf"><embed src="/images/default/banners/ucl_tranzitional_550x550px.swf" width="550" height="550"></embed></object>';
		//html += '</div>';
	//	html += '<div style="font-size:12px;color:#fff;padding:10 5px 3px 5px;text-align:center;">Aceasta pagina contine un mesaj promotional care va ramane pe ecran maximum 15 secunde!';
	//	html += '<br /><a href="javascript:;" style="color:#fff;font-weight:bold;"class="rezervaNext">Click aici pentru a accesa instant pagina dorita!</a></div>';
		var _left = ($('body').width() - 580) / 2;
		_left = _left + 'px';
		
		$.blockUI(
		{
			css : 
			{
				padding: 10,
				top: '15px',
				left : _left,
				width : 580,
				height: 620,
				backgroundColor : '#0a3d1e',
				border : '3px solid #fff',
				zIndex : '100000'

			},		
			message : html
		});
	}
	
		var lnk = $('<a>').attr('href','javascript:;').attr('id','lnk').css('display','none');
		lnk.appendTo('body');		
		lnk.bind('click', function()
		{
		
			var newWin;				
			newWin = window.open("/tickets/?siteId=" + App.cine + "&showId="+id,"CPL","width=870,height=670,scrollbars=1,menubar=no,resizable=no,top=" + ( screen.width - 770 ) / 2 + ",left=" + ( screen.height - 570 ) / 2 + ",status=no");
			if (newWin)
			{
				newWin.focus(); 
			}		
		});	
	
	var openRezervaWin = function()
	{
		if (adSeen == null && testBanner > -1)
		{
			$.unblockUI();		
		}
	
		lnk.click();
	}

	$('.rezervaNext').click(function() { 
		openRezervaWin();
	});
		
	if (adSeen == null && testBanner > -1)
	{
		setTimeout(function() {
			openRezervaWin();
			}, 12000);
	}
	else
	{
		openRezervaWin();
	}
}

	
	
	


