
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').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;
			}
			GUI.load(url, null, null, null, 'no', 'yes');
			return false;
		});
	};
};

cGUI.prototype.postLoad = function() {
	$('body').css({opacity:1, cursor: 'default'});
	GUI.parseLinks();
	if (typeof userInfo == 'undefined') {
		$.getScript(ROOT + 'js/sifr-config.js');
		$.getScript(ROOT + 'js/cufon-config.js');
	}
};

cGUI.prototype.load = function(url, target, source, callback, reurl, history, from_flash) {

	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 (typeof pageTracker != 'undefined') {pageTracker._trackPageview(url);}

	if (reurl == 'yes') url = '/ajax' + url + '/' + source;

	$.post(url, function(data){
			if (reurl == 'yes') {
				$('#' + target).html(data);
			} else {
				$('#' + target).html($(data).find('#' + source).html());
			}
			GUI.postLoad();
			eval(callback);
			setTimeout(function(){
				$('#throbber').hide();
			}, 100);
	});
	void(0);
};

var cPage = function() {

	this.init = function() {

//		GUI.parseLinks();
//		$.historyInit(pageload);
//		alert(ROOT);

	};
	this.faq = function(){
			$('.module_faq .item div.c_item').hide();
			$('.module_faq h3 a').click(function(){
				if($(this).parents('.item').children('div.c_item').css('display') == 'none'){
					$(this).parents('.item').children('div.c_item').slideDown();
					$(this).parents('.item').addClass('active');
				} else {
					$(this).parents('.item').children('div.c_item').slideUp();
					$(this).parents('.item').removeClass('active');
				};
				return false;
			});
	};

	this.pagePath = window.location.pathname;

	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>');
	}
	this.tableRows = function() {
		$('#text_box table tr').each( function() {
				if($(this).parent().parent().hasClass("normal")||$(this).parent().parent().hasClass("images"))
					{
					return;
					}
				if( !$(this).hasClass("dark") && !$(this).prev("tr").hasClass("dark")) {
					$(this).addClass("dark");
				}
				else {
					//$(this).addClass("dark");
				};
			}
		)
	}
	this.promoFlash = function(param) {
		//alert('Works! ' + param);
		if (typeof pageTracker != 'undefined') {pageTracker._trackPageview('hp-flash/okna-na-splatky/priklad-'+param);}
	};

};
var PAGE = new cPage();

$(function(){
	$("#detail_search_frm").hide();
	PAGE.init();
	if (typeof userInfo == 'undefined') {
		PAGE.faq();
		PAGE.tableRows();
	}

	MENU.init();
	/*
	$("#search_form_text").autocomplete(ROOT + LANG.search_page + ".html/", {
 		width:"205px",
			autoFill:true,
			minChars:3,
			selectFirst:false,
			scroll:true
		}
	);
	*/

	$("a.detail_search").click(function () {
	      $("#detail_search_frm").slideToggle("slow");
	    });

	$('#search_form_text, #search_text').bind('focus', function(){
		if ($(this).attr('title') == $(this).val()) $(this).val('');
	});
	$('#search_form_text, #search_text').bind('blur', function(){
		if ($(this).val() == '') $(this).val($(this).attr('title'));
	});

	$("form.forms input[type='checkbox'][class!='product'][class!='nocustom']").custCheckBox({});

	$('a.print').bind('click', function(){
		window.print();
		return false;
	});

	if (HTMLinfo.page_name.indexOf('kontakt') != 0) $("ul#submenu a").each( function(){
		href = $(this).attr("href");
		href = href.substr(0, href.length - 5);
		path = PAGE.pagePath;
		path = path.substr(0, path.length - 5);
		if (path.indexOf(href) != -1 ){
			$(this).addClass("active");
		};
	});

	$('#submenu li ul').each(function(){
		$(this).find('li a img[src$="default.gif"]').parent().parent().parent().css({
			'margin-left': '15px'
		});
	});

	$('a.lightbox').lightBox({
		imageLoading:			'/themes/images/lightbox/lightbox-ico-loading.gif',
		imageBtnPrev:			'/themes/images/lightbox/lightbox-btn-prev.gif',
		imageBtnNext:			'/themes/images/lightbox/lightbox-btn-next.gif',
		imageBtnClose:			'/themes/images/lightbox/lightbox-btn-close.gif',
		imageBlank:				'/themes/images/lightbox/lightbox-blank.gif'
	});

	$(".carousel[rel^=run]").each(function(){
		_rel = $(this).attr('rel');
		tmp = _rel.split('_');
		$(this).jCarouselLite({
	        btnNext: ".next",
	        btnPrev: ".prev",
			visible: parseInt(tmp[1])
	    })
	});

	//if (typeof userInfo == 'undefined' && typeof $.ui != 'undefined' && typeof $.ui.tabs != 'undefined') 
	$(".tabs").tabs({
		show: function(event, ui) {
			$(ui.tab).parent().parent().find('li:not([class~=last]) a').css({
				'background-image': 'url(/themes/images/bg_menu_border.gif)'
			});
			$(ui.tab).parent().prev().find('a').css({
				'background-image': 'none'
			});
			$(ui.tab).css({
				'background-image': 'none'
			});
		}
	});

	if (typeof $.ui != 'undefined' && typeof $.ui.accordion != 'undefined') $("#accordion").accordion({
		autoHeight: true,
		navigation: true
	});

	$('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;
	});
});

var cMenu = function() {
	var timer;
	var trigger;
	var currMenu;
	this.init = function() {
		this.trigger = false;
		this.obj = null;
		$('ul#menu li a:not(li ul li a)').hover(function(){
			clearTimeout(MENU.timer);
			$('ul#menu li a[class!=active]:not(li ul li a)').css({
				'background-image' : 'url(/themes/images/bg_menu_border.gif)',
				'background-color' : '#DD1641'
			});
			$(this).parent().prev().children('a').css({'background-image' : 'none'});
			$(this).css({'background-image' : 'none', 'background-color' : '#262626'});
			if (MENU.trigger == false || MENU.obj.attr('id') != $(this).parent().children("div").attr('id')) {
				//$("ul#menu li div:not(li ul li div)").fadeOut();
				$("ul#menu li div:not(li ul li div)").hide();
				//$(this).parent().children("div").fadeIn();
				$(this).parent().children("div").show();
			}
			MENU.currMenu = $(this);
			MENU.obj = $(this).parent().children("div");
			MENU.main =$(this);
			MENU.trigger = false;
		}, function(){
			MENU.timer = setTimeout(function(){
				//MENU.obj.fadeOut();
				MENU.obj.hide();
				//console.log(MENU.main.parent().attr('class'));
				if (MENU.main.parent().attr('class') != 'last') MENU.main.css({
					'background-image' : 'url(/themes/images/bg_menu_border.gif)'
				});
				MENU.main.css({
					'background-color' : '#DD1641'
				});
				MENU.main.parent().prev().children('a').css({
					'background-image' : 'url(/themes/images/bg_menu_border.gif)'
				});

				$('ul#menu li a.active:not(li ul li a)').css({
					'background-image' : 'none',
					'background-color' : '#262626'
				});
				$('ul#menu li a.active:not(li ul li a)').parent().prev().children('a').css({
					'background-image' : 'none'
				});
			}, 50);
		});
		$('ul#menu li a.active:not(li ul li a)').parent().prev().find('a').css({
			'background-image' : 'none'
		});
		$("ul#menu li div:not(li ul li div)").hover(function(){
			clearTimeout(MENU.timer);
		}, function(){
			//MENU.trigger = true;
			MENU.trigger = false;
			$(this).prev().trigger('mouseout');
		});
	};
};
var MENU = new cMenu();


$(function() {

		if ($.browser.msie) if (parseInt($.browser.version) <= 6) return;

		if (typeof userInfo != 'undefined') return;

		if (typeof $.ui != 'undefined' && typeof $.ui.slider != 'undefined') {

			var scrollPane = $('.scroll-pane');
			var scrollContent = $('.scroll-content');

			var scrollbar = $(".scroll-bar").slider({
				slide:function(e, ui){
					if( scrollContent.width() > scrollPane.width() ){ scrollContent.css('margin-left', Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() ) + 65) + 'px'); }
					else { scrollContent.css('margin-left', 0); }
				}
			});

			var vscrollbar = $(".vscroll-bar1").slider({
				orientation: 'vertical',
				slide:function(e, ui){
				log(ui.value);
					if( scrollContent.height() > scrollPane.height() ){ scrollContent.css('margin-top', Math.round( ui.value / 100 * ( scrollPane.height() - scrollContent.height() )) + 'px'); }
					else { scrollContent.css('margin-top', 0); }
				}
			});

			var handleHelper = scrollbar.find('.ui-slider-handle')
			.mousedown(function(){
				scrollbar.width( '100%');//handleHelper.width() );
			})
			.mouseup(function(){
				scrollbar.width( '100%' );
			})
			.append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
			.wrap('<div class="ui-handle-helper-parent"></div>').parent();

			var vhandleHelper = vscrollbar.find('.ui-slider-handle')
			.mousedown(function(){
				vscrollbar.height( '100%');//vhandleHelper.height() );
			})
			.mouseup(function(){
				vscrollbar.height( '100%' );
			})
			.append('<span class="ui-icon ui-icon-grip-dotted-horizontal"></span>')
			.wrap('<div class="ui-handle-helper-parent"></div>').parent();

			scrollPane.css('overflow','hidden');

			function sizeScrollbar(){

				var remainder = scrollContent.width() - scrollPane.width();
				var proportion = remainder / scrollContent.width();
				var handleSize = scrollPane.width() - (proportion * scrollPane.width());
				scrollbar.find('.ui-slider-handle').css({
					width: handleSize,
					'margin-left': -handleSize/2
				});
				handleHelper.width('').width( scrollbar.width() - handleSize);

				var remainder = scrollContent.height() - scrollPane.height();
				var proportion = remainder / scrollContent.height();
				var handleSize = scrollPane.height() - (proportion * scrollPane.height());
				vscrollbar.find('.ui-slider-handle').css({
					height: handleSize,
					'margin-top': -handleSize/2
				});
				vhandleHelper.height('').height( vscrollbar.height() - handleSize);

			}

			function resetValue(){

				var remainder = scrollPane.width() - scrollContent.width();
				var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
				var percentage = Math.round(leftVal / remainder * 100);
				scrollbar.slider("value", percentage);

				var remainder = scrollPane.height() - scrollContent.height();
				var topVal = scrollContent.css('margin-top') == 'auto' ? 0 : parseInt(scrollContent.css('margin-top'));
				var percentage = Math.round(topVal / remainder * 100);
				vscrollbar.slider("value", percentage);

			}

			function reflowContent(){

					var showing = scrollContent.width() + parseInt( scrollContent.css('margin-left') );
					var gap = scrollPane.width() - showing;
					if(gap > 0){
						scrollContent.css('margin-left', parseInt( scrollContent.css('margin-left') ) + gap);
					}

					var showing = scrollContent.height() + parseInt( scrollContent.css('margin-top') );
					var gap = scrollPane.height() - showing;
					if(gap > 0){
						scrollContent.css('margin-top', parseInt( scrollContent.css('margin-top') ) + gap);
					}

			}

			$(window)
			.resize(function(){
					resetValue();
					sizeScrollbar();
					reflowContent();
			});
			setTimeout(sizeScrollbar,10);
		}
	});
