
var cPage = function() {

	this.web2 = false;
	
	this.init = function() {
		this.load_func_starter = 'no';
		this.queue = [];
        
        $(window).bind('beforeunload', function(){ 
            PAGE.unload();
        });        
        
	};
	
	this.onload = function(){
	    setTimeout(function(){
	        $('.throbber').remove();
	    }, 100);
	    $('body').css({
	        cursor: 'default'
	    });
	    
		$(".contact_form").hide();
		$('a.lightbox').lightBox();
		$('a.lightbox_single').each(function(){
			$(this).lightBox();
		});
		$.addwatermarks();

        $('form.initRequired').validate({
			focusCleanup: true,
			focusInvalid: false,
			errorElement: "em"
		});

        if($('form.subscribtionForm').length > 0) {
            var subscriptionVal = $('form.subscribtionForm input.subscribe_email').attr('title');
            $('form.subscribtionForm input.subscribe_email').focus(function() {
                if(subscriptionVal == $('form.subscribtionForm input.subscribe_email').val()) {
                    $(this).val('');
                }
            });
            $('form.subscribtionForm input.subscribe_email').focusout(function() {
                if($(this).val() == '') {
                    $(this).val(subscriptionVal);
                }
            });
            $('form.subscribtionForm').unbind('submit').bind('submit', function() {
                if($('form.subscribtionForm input.subscribe_email').val() == '') {
                    if(HTMLinfo.lang == 'cz') {
                        $('form.subscribtionForm input.subscribe_email').val(subscriptionVal);
                    }
                    return false;
                }

                if ($('form.subscribtionForm input.subscribe_email').val() == subscriptionVal) {
                    return false;
                }
            });
        }

        $('.gridBox:has(a)').each(function(){
        	if (!$(this).hasClass('cinema')) $(this).unbind('click').bind('click',function(){
        		if (PAGE.web2 === true) {
        			$($('a', this).get(0)).trigger('click');
        			return false;
        		} else {
        			var href = $($('a', this).get(0)).attr('href');
        			window.location = href;
        		}
        	}).css({'cursor': 'pointer'});
        });

        if(window.location.hash == '#form') {
            $(".contact_form").show();
        }

        $("a.show_form").click(function () {
	      $(".contact_form").slideToggle("slow");
	    });
        
    	$('a.print').bind('click', function(){
    		window.print();
    		return false;
    	});
    	
		if (typeof $.ifixpng != 'undefined') $('*').ifixpng(ROOT + 'themes/images/blank.gif');

//        if (HTMLinfo.page_name == '�ndex') {
            var e = document.createElement('script');
            e.type = 'text/javascript';
            if (HTMLinfo.lang == 'en') fb_lng = '//connect.facebook.net/en_US/all.js';
                else fb_lng = '//connect.facebook.net/cs_CZ/all.js';
            e.src = document.location.protocol + fb_lng;
            e.async = true;
            if (!document.getElementById('fb-root')) {} else {
                document.getElementById('fb-root').appendChild(e);
            };
//        }

		if (typeof userInfo == 'undefined') {
			if (this.web2 === true) {
				$('#page a').not('[class~="lightbox"]').unbind('click').bind('click', function(){
					PAGE.load($(this).attr('href'), null, null, null, null, 'yes');
					return false;
				});
			}
			$('a.ajax').unbind('click').bind('click', function(){
				PAGE.load_func_starter = 'yes';
				PAGE.load($(this).attr('href'), null, '#left_box', '#left_box', null, null, 'no');
				return false;
			});
			
			/*
			$('a.ajax_history').unbind('click').bind('click', function(){
				PAGE.load($(this).attr('href'));
				return false;
			});
			$('form').unbind('submit').bind('submit', function(){
				url = $(this).attr('action');
				pars = $(this).serialize();
				
				gatrack(url);
				$.history.load(url);

				target = '#content';
				source = '#content'
				
				$.post(url, pars, function(data){
					$(target).html($(source, data).html());
					PAGE.onload();
					log('form');
				});
				
				return false;
			});
			*/
		};
	};
	
	this.pageLoad = function() {
		
		if (this.web2 === true) PAGE.historyInit();
		
		PAGE.onload();
		
	};
	
	this.addQ = function(fnc) {
//		this.queue.push(fnc);
//		eval(fnc);
//		log(this.queue.length);
	};
	
	this.historyInit = function(){
		if (typeof userInfo == 'undefined') {
			tmp = window.location.hash;
            tmp = tmp.replace(/^.*#/, '');
            PAGE.start_hash_test = PAGE.hash_test = tmp;
            if (PAGE.start_hash_test != window.location.pathname) {
            	PAGE.start_hash_test = window.location.pathname;
            }
			$.history.init(function(hash){
		        if(hash == "") {
		            window.location.hash = '#' + window.location.pathname;
		            tmp = window.location.hash;
		            tmp = tmp.replace(/^.*#/, '');
		        	PAGE.hash_test = tmp;
		        } else if ( hash != PAGE.hash_test || window.location.pathname != PAGE.hash_test) {
		        	PAGE.hash_test = hash;
	        		PAGE.load(hash, null, null, null, null, 'yes', 'no');
		        }
		    },
		    {unescape: ",/"});
		};
	};
	
	this.load = function(url, pars, target, source, callback, reurl, history) {
		if (!target) target = '#page';
		if (!source) source = '#page';
		if (!callback) callback = 'void(0);';
		if (!reurl) reurl = 'no';
		if (!history) history = 'yes';

		if (url.indexOf(window.location.hostname) != -1) {
			url = (url.substr(url.indexOf(window.location.hostname) + window.location.hostname.length));
		}
		
		if (reurl == 'yes') {
			PAGE.reurl = reurl;
		}
		PAGE.reurl = reurl;
		
		if (history == 'yes') {
			gatrack(url);
			$.history.load(url, null, null, null, null, 'no');
			return false;
		}

		if (PAGE.reurl == 'yes') {
			url = '/ajax' + url + '/' + source.substr(1);
			PAGE.load_func_starter = 'yes';
		}

		$("#page a").unbind('click');
		$('#preloader').fadeIn();
        $('body').css({'cursor': 'wait'});
        w = $(document).width();
        h = $(document).height();			
		$('.throbber').remove();
		$('body').append('<div class="throbber" style="z-index: 110; cursor: wait; opacity: 0.2; position: absolute; width: ' + w + 'px; height: ' + h + 'px; top: 0px; left: 0px; background: transparent;">&nbsp;</div>');
		
		if (PAGE.load_func_starter == 'yes') {
			//PAGE.throbber(target, 300);
		}
    		
		
		$.post(url, pars, function(data){
			if (window.location.hash != '#' + window.location.pathname || PAGE.load_func_starter == 'yes') {
				//$('#interactive').html($('#interactive', data).html());
				if (PAGE.reurl == 'yes') {
					//$('body').attr('id')
					pos = data.indexOf('|');
					$('body').attr('id', data.substr(0, pos));
					$(target).html(data.substr(pos + 1));
				} else {
					$(target).html($(source, data).html());
				}
				$('#preloader').hide();
				PAGE.onload();
			} else {
				PAGE.load_func_starter = 'yes';
			}
//			log('a');
		});
		
	};
	
	this.pagePath = window.location.pathname;

	this.block = function() {
		$('#systemWorking').fadeIn();
	};
	this.unblock = function() {
		$('#systemWorking').fadeOut();
	};
	this.throbber = function(id, top) {
//		$(id).html('<center style="padding-top: 50px;"><img src="' + ROOT + 'themes/images/cms/throbber.gif"/></center>');
        if (!top) top = 10;
		$(id).css({
			'position': 'relative'
		});
        left = Math.round($(id).width() / 2);
        if (!left || left < 1) left = 350;
		$('<div style="position: absolute; top: ' + top + 'px; left: ' + left + 'px;"><img src="' + ROOT + 'themes/images/cms/throbber.gif"/></div>').appendTo(id);
	};
    this.unload = function(){
        $('#page').fadeOut(function(){
//            $('#preloader').fadeIn();
        });
        $('#preloader').fadeIn();
        
        /*
        $('#content').html('&nbsp;').height(150);
        PAGE.throbber('#content', 100);
        */
    }
};
var PAGE = new cPage();

PAGE.init();

$(function(){
	PAGE.pageLoad();
});

var gatrack = function(url) {
	if (typeof _gaq == 'undefined') {} else {_gaq.push(['_trackPageview', url]);}
	void(0);
};

window.fbAsyncInit = function() {
	FB.init({
//		appId   : facebook.appId,
//		session : facebook.session, // don't refetch the session when PHP already has it
		status  : false, // check login status
		logging	: false,
		cookie  : false, // enable cookies to allow the server to access the session
		xfbml   : true // parse XFBML	
	});
}
