
hash_test = "";
function pageload(hash, target, source, callback, reurl, history) {
	if (!target) target = 'middle_box';
	if (!source) source = 'middle_box';
	if (!callback) callback = 'void(0);';
	if (!reurl) reurl = 'yes';
	if (!history) history = 'yes';
	if(hash) {
		if ( hash != GUI.hash_test) {
			GUI.hash_test = hash;
			GUI.load(hash, target, source,  callback, reurl, 'no');
		};
	} else {
		$("#load").empty();
	}
}

cGUI.prototype.parseLinks = function() {
	if (typeof userInfo == 'undefined') {
		$('#middle_box a, #bottom_box a, #menu a, #blog').unbind('click');
		$('#middle_box a, #bottom_box a').bind('click', function(){

			url = $(this).attr('href');

			if ($(this).attr('target') == '_blank' || url.substr(0, 7) == 'mailto:') {
				return true;
			}

//			setTimeout(function(){
				GUI.load(url, null, null, null, 'no', 'yes');
//			}, 1);
			return false;
		});
		$('#blog').unbind();
		$('#katalog-menu-opener').unbind();
		$('#menu a').bind('click', function(){
			url = $(this).attr('href');
			GUI.load(url, 'menu', 'menu', "GUI.load('" + url + "', null, null, null, 'no', 'yes');", 'no', 'no');
			return false;
		});
	};
	$('.news_gallery a').lightBox();

//	$('.news_gallery a:first').trigger('click');
};

cGUI.prototype.postLoad = function() {
	$('body').css({opacity:1, cursor: 'default'});
	GUI.parseLinks();
	$.getScript(ROOT + 'js/sifr-config.js');
};

cGUI.prototype.URLchangeFix = function(url) {

		if (typeof userInfo != 'undefined') {
			return;
		}

//	console.log(typeof thisMovie('MaxinPrague').URLchange);

	if (typeof thisMovie('MaxinPrague').URLchange != 'function') {
		setTimeout(function(){
			GUI.URLchangeFix(url);
		}, 10);
		return false;
	}

	thisMovie('MaxinPrague').URLchange(url);

};

cGUI.prototype.load = function(url, target, source, callback, reurl, history, from_flash) {
	
	if (url.search("blog/")>-1) {
		window.location='http://www.maxin-prague.cz/blog/';
		return false;
	}
	if (url.search("catalog/")>-1) {
		window.location='http://www.maxin-prague.cz/catalog/';
		return false;
	}
	if (!target) target = 'middle_box';
	if (!source) source = 'middle_box';
	if (!callback) callback = 'void(0);';
	if (!reurl) reurl = 'yes';
	if (!history) history = 'yes';
	if (!from_flash) from_flash = 'no';

	reurl = 'no';

	if (url.indexOf(window.location.hostname) != -1) {
		url = (url.substr(url.indexOf(window.location.hostname) + window.location.hostname.length));
	}

	if (history != 'no') {
		url = url.replace(/^.*#/, '');
		$.historyLoad(url, target, source, callback, reurl, history);
		return false;
	}

	if (from_flash != 'yes') {
		GUI.URLchangeFix(url);
	}

	if (typeof pageTracker != 'undefined') {pageTracker._trackPageview(url);}

	if (reurl == 'yes') url = '/ajax' + url + '/' + source;

	if (target == 'middle_box') {
		$('#' + target).css({position: 'relative'});
		$('#' + target).append('<img src="'+ROOT+'themes/images/ajax-loader.gif" style="z-index: 1001; position: absolute; top: 20px; left: 290px;"/>');
		$('#' + target).append('<div class="loadingContent" style="z-index: 1; position: absolute; top: 0px; left: 0px; background: #bbb; width: ' + $('#' + target).width() + 'px; height: ' + $('#' + target).height() + 'px;"></div>');
		$('.loadingContent').css({opacity: 0.7, cursor: 'wait', zIndex: 1000});
		$('body').css({opacity: 0.4, cursor: 'wait'});
	};
	
	$.post(url, function(data){
			if (reurl == 'yes') {
				$('#' + target).html(data);
			} else {
				$('#' + target).html($(data).find('#' + source).html());
			}
			p = data.indexOf(find = '<title>');
			title = data.substr(p + find.length);
			p = title.indexOf(find = '</title>');
			title = title.substr(0, p);
			title = title.trim();
			try {
  				window.title = title;
  				document.title = title;
  			} catch(err) {}

			//change background images in bottom_prom
			var lang_mut="_"+url.substr(1,2);
			var span_height;
			var ico_sitemap_w;
			var ico_sitemap_x_y;
			var ico_sitemap_x_y_h;
			var ico_promo_video_w;
			var ico_promo_video_x_y;
			var ico_promo_video_x_y_h;
			switch(lang_mut)
				{
				case "_cz":
					$(".bottom_promo").css("height","221px");
					span_height="34px";
					ico_sitemap_w="55px";
					ico_sitemap_x_y="-359px -20px";
					ico_sitemap_x_y_h="-359px 0";
					ico_promo_video_w="84px";
					ico_promo_video_x_y="-246px -20px";
					ico_promo_video_x_y_h="-246px 0";
				break;
				case "_en":
					span_height="31px";
					ico_sitemap_w="55px";
					ico_sitemap_x_y="-357px -20px";
					ico_sitemap_x_y_h="-357px 0";
					ico_promo_video_w="84px";
					ico_promo_video_x_y="-246px -20px";
					ico_promo_video_x_y_h="-246px 0";
				break;
				case "_fr":
					$(".bottom_promo").css("height","221px");
					span_height="34px";
					ico_sitemap_w="76px";
					ico_sitemap_x_y="-336px -20px";
					ico_sitemap_x_y_h="-336px 0";
					ico_promo_video_w="46px";
					ico_promo_video_x_y="-258px -20px";
					ico_promo_video_x_y_h="-258px 0";
				break;
				case "_se":
					span_height="33px";
					ico_sitemap_w="135px";
					ico_sitemap_x_y="-295px -20px";
					ico_sitemap_x_y_h="-295px 0";
					ico_promo_video_w="80px";
					ico_promo_video_x_y="-206px -20px";
					ico_promo_video_x_y_h="-206px 0";
				break;
				}
			$(".bottom_promo a span").each(function()
				{
				//$(this).css("height",span_height);
				//$(this).css("background-image","url(/themes/images/bg_home_promo"+lang_mut+".gif)");
				});
			$("#bottom_box ul.right a").each(function()
				{
				$(this).css("background-image","url(/themes/images/icon_footer"+lang_mut+".gif)");
				});
			$("#ico_promo_video").css("width",ico_promo_video_w);
			$("#ico_promo_video").css("background-position",ico_promo_video_x_y);
			$("#ico_sitemap").css("width",ico_sitemap_w);
			$("#ico_sitemap").css("background-position",ico_sitemap_x_y);
			$("#ico_promo_video").hover(
				function()
					{
     				$(this).css("background-position",ico_promo_video_x_y_h);
					},
				function()
					{
					$(this).css("background-position",ico_promo_video_x_y);
					});
			$("#ico_sitemap").hover(
				function()
					{
     				$(this).css("background-position",ico_sitemap_x_y_h);
					},
				function()
					{
					$(this).css("background-position",ico_sitemap_x_y);
					});

  			//end change background images in bottom_promo

			GUI.postLoad();
			eval(callback);
			setTimeout(function(){
				$('#throbber').hide();
			}, 100);
	});
	void(0);
};

var cPage = function() {

	this.init = function() {

		GUI.parseLinks();
//		GUI.last_url = '/en/index.html';

		if (typeof $.ifixpng != 'undefined') $('*').ifixpng(ROOT + 'themes/images/blank.gif');

		$.historyInit(pageload);

		PAGE.initFlash();
		if($("body").width() <= 1100 || $(window).height() >= 750){
			$("#scroll").hide();
		} else {
			$("#scroll").show();
		};
		$(window).resize(function() {
			if($("body").width() <= 1100 || $(window).height() >= 750){
				$("#scroll").hide();
			} else {
				$("#scroll").show();
			}
		});

//		alert(window.location.hash);

//		GUI.last_url = '';
//		GUI.hash_test = '';

	};

	this.initFlash = function() {

		if (typeof userInfo != 'undefined') {
			return;
		}

//		console.log(typeof thisMovie('MaxinPrague').URLchange);

		if (typeof thisMovie('MaxinPrague').URLchange != 'function') {
			setTimeout(function(){
				PAGE.initFlash();
			}, 10);
			return false;
		}

		if (window.location.hash) {
//			url = window.location.hash;
//			url = url.substr(1);
		} else {
			GUI.hash_test = '/' + HTMLinfo.lang + '/' + HTMLinfo.page_name + '.html';
			url = '/' + HTMLinfo.lang + '/' + HTMLinfo.page_name + '.html';
//			alert('call to flash: thisMovie(\'MaxinPrague\').URLchange(\''+url+'\')');
//			setTimeout(function(){
				thisMovie('MaxinPrague').URLchange(url);
//			}, 100);
			window.location.hash = '#/' + HTMLinfo.lang + '/' + HTMLinfo.page_name + '.html';
		}

	};

	this.block = function() {
		$('#systemWorking').fadeIn();
	};
	this.unblock = function() {
		$('#systemWorking').fadeOut();
	};
	this.throbber = function(id) {
		$(id).html('<center style="padding-top: 50px;"><img src="' + ROOT + 'themes/images/cms/throbber.gif"/></center>');
	}

};
var PAGE = new cPage();

$(function(){
	PAGE.init();

	$('a[rel=facebox]').bind('click', function(){
		url = $(this).attr('href');
		$.post(url, null, function(data, textStatus) {
			_body = $(data).find('#text_box').html();
			$.facebox(_body);
		});
		return false;
	});
	$('a[rel=facebox-image]').bind('click', function(){
		var url = $(this).attr('href');
		$.facebox(function() {
			data = '<img src="' + url + '"/>';
			$.facebox('<div style="width:600px;"><center>' + data +'</center></div>');
		});
		return false;

	});

	$('a[rel^=facebox-int]').bind('click', function(){
		url = $(this).attr('href');
		_rel = $(this).attr('rel');
		$.post(url, null, function(data, textStatus) {
			_body = $(data).find('#content').html();
			$.facebox('<div style="height: auto; overflow-y:auto;">' + _body +'</div>');
		});
		return false;
	});


/*	$('a[rel^=facebox-ext]').bind('click', function(){
		url = $(this).attr('href');
		_rel = $(this).attr('rel');
		tmp = _rel.split('-');
		if (typeof tmp[2] != 'undefined' && typeof tmp[3] != 'undefined') {
			$.facebox('<iframe frameborder="0" src="' + url + '" width="' + tmp[2] + '" height="' + tmp[3] + '"> </iframe>');
		} else {
			$.facebox('<iframe frameborder="0" src="' + url + '" width="100%" height="100%"> </iframe>');
		}
		return false;
	});*/
});


function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

