var cMenu = function() {
	var timer;
	var currMenu;
	this.init = function() {
		$(".prev,.next").mouseover(function () {
		      $(this).trigger('click');
    	});
		if (HTMLinfo.page_name == 'liscata') {
			liscataHook();
		} else {
			$("#promo ul li, .advert_box").css({'cursor': 'pointer'}).click(function(){
				href = $(this).find(".but_more").attr("href"); 
				window.location = href;
			});
		}
		$("#menu li a:not(li ul li a)").each(
				function() {
					if ( !$(this).hasClass("no_border") ) {
						$(this).hover(
							function(){
								$("#submenu1, #submenu2, #submenu3").hide();
								$(this).parent().children("div").show();
								$(this).css({"border":"1px solid #E7E7E7", "border-bottom":"none"});
								MENU.currMenu = $(this);
								MENU.obj = $(this).parent().children("div");	
								clearTimeout(MENU.timer);
								},
							function(){
								MENU.currMenu.css({"border":"1px solid #FFFFFF", "border-bottom":"none"});
								MENU.timer = setTimeout(function(){MENU.obj.hide(); },100);
							}
						);
					};
				}
		);
		$("#submenu1, #submenu2, #submenu3").mouseover(
				function() {
					clearTimeout(MENU.timer);
					$(this).show();
					MENU.currMenu.css({"border":"1px solid #E7E7E7", "border-bottom":"none"});
				}
		);
		$("#submenu1, #submenu2, #submenu3").mouseout(
				function() {
					$(this).hide();
					MENU.currMenu.css({"border":"1px solid #FFFFFF", "border-bottom":"none"});
				}
		);
		if (typeof $.ifixpng != 'undefined') $('*').ifixpng(ROOT + 'themes/images/blank.gif');
	};	
};
var MENU = new cMenu();



var cPage = function() {
	this.interactiveTime;
	this.interactiveState = 1;
	this.interactiveEnable = false;
	this.contactFormOpen = false;
	this.init = function() {

		jQuery('#interactive_menu').accordion({
				active: true, autoHeight: false, collapsible:true, event: 'mouseover',header: '.head', navigation: true
		});
		$('#interactive_menu div ul li a').click(function(){
			pageTracker._trackPageview('/interactivemenu' + $(this).attr("href"));
		});
		$('#hp_big_promo .close').click(function(){
			$('#hp_big_promo').fadeOut();
			return false;
		});
		$('#interactive_menu div').css({"opacity":"0.85"});
		$('#menu div').css({"opacity":"0.93"});
		
		$('#interactive_menu').hover( 
			function(){	
				clearTimeout(PAGE.interactiveTime);
				PAGE.openInteractive();	
			}, 
			function() {
				PAGE.interactiveTime = setTimeout(function(){PAGE.closeInteractive();},100);
			}
		);
		PAGE.tableRows();
/*		$('#interactive_menu_box').mouseout( function(){
			if (PAGE.interactiveState == 1) {									  
				PAGE.closeInteractive();
				PAGE.interactiveState = 0;
			}
		});
*/
		$('input[type="text"]').focus( function() {
			if ($(this).attr('rel') == '' || typeof($(this).attr('rel')) == 'undefined' ) {
				$(this).attr("rel",$(this).val());									
				$(this).val("");
				if ($(this).attr('id') == 'search_text') {
					$('#header form input.submit').css({
						'background-image': 'url('+ROOT+'themes/images/bg_submit.gif)'
					});
					$('#header form input.submit').unbind('click');
				}
			}
			
		});
//		$('input[type="submit"], input[type="reset"], input[type="button"]').val("");
		$('input[type="text"]').blur( function() {
			if ( $(this).val() == "" ) {
				$(this).val($(this).attr("rel"));
				$(this).attr("rel", '');	
			}
			
		});

		if (HTMLinfo.page_name == 'liscata') {
			if (typeof userInfo == 'undefined') {
				$("#promo").jCarouselLite({
					btnNext: ".next",
					btnPrev: ".prev",
					visible: 3,
					scroll: 2,
					auto: 6000,
					circular: true,
					speed: 1800,
					easing: 'easeinout',
					beforeStart: function(){
						liscataHook();
					},
					afterEnd: function(){
						liscataHook();
					}
			    });
			};
			$('#inter_right .close').unbind('click').bind('click', function(){
				$('#inter_right').hide();
				return false;
			});
			
		} else {

			if (typeof userInfo == 'undefined') {
				$("#promo").jCarouselLite({
					btnNext: ".next",
					btnPrev: ".prev",
					visible: 3,
					scroll: 3,
					auto: 6000,
					circular: true,
					speed: 1800,
					easing: 'easeinout'
			    });
			    /*
			    $('#promo ul li a.but_more').each(function(){
			    	$(this).parent('li').unbind('click').bind('click', function(){
			    		console.log($(this).find('a.but_more').attr('href'));
			    		return false;
			    	});
			    });
			    */
			};

		}
		
		
		//*/
		$("#form_seznam_firem tr.detail").hide();
		$("#form_seznam_firem table tr.dark").css("cursor","pointer");
		$("#form_seznam_firem table tr.dark").click(
				function() {
					if ($(this).next().css("display") == "none") {
						$(this).next().show();
					} else {
						$(this).next().hide();
					}
				}
		);
		$(".advisor_search").autocomplete(ROOT + HTMLinfo.lang + "/_autocomplete.html/", {
		 		width:"205px",
				autoFill:true,
				minChars:1,
				selectFirst:false,
				scroll:false
			}
		);
		$("#search_text").autocomplete(ROOT + HTMLinfo.lang + "/"+ LANG.search_page +".html/", {
		 		width:"205px",
				autoFill:true,
				minChars:3,
				selectFirst:false,
				scroll:true
			}
		);
		
		
		
		$('<div id="systemWorking"><img src="' + ROOT + 'themes/images/cms/loading.gif"/></div>').appendTo(document.body);
		$("tr.detail_contact td").hide();
		 $('.tip').hide();
		$("a#show_detail_contact").click(function () {
			if (PAGE.contactFormOpen == true) {
				 $("tr.detail_contact td").hide();
				 $('.tip').hide();
				 $(this).removeClass("detail_close");
				 $(this).addClass("detail_open");
				 $(this).html("Chci doplnit podrobnější údaje o sobě (např. adresa nebo číslo smlouvy)");
				 PAGE.contactFormOpen = false;
			}else {
				if ($.browser.msie) {
					$("tr.detail_contact td").css({'display':'inline'});
				 } else {
					 $("tr.detail_contact td").css({'display':'table-cell'});
				};
				
				$('.tip').show();
				 $(this).removeClass("detail_open");
				$(this).addClass("detail_close");
				$(this).html("Schovat doplňující informace");
				 PAGE.contactFormOpen = true;
			};
		   return false;
		  });
		
	};
	this.openInteractive = function(){
		if (PAGE.interactiveState == 0 && PAGE.interactiveEnable == true ) {
			$('#interactive_menu_box').animate({"width":"220px"}, function(){
				PAGE.interactiveState = 1;
			});
		};
	};
	this.closeInteractive = function(){
		if (PAGE.interactiveState == 1 && PAGE.interactiveEnable == true) {
				$('#interactive_menu_box').animate({"width":"40px"}, function(){
				PAGE.interactiveState = 0;
		});			
		};
		jQuery('#interactive_menu').accordion( 'activate', 4 );
	};
	this.closeInteractiveFromFlash = function(){
		//alert("closed from flash");
		PAGE.interactiveEnable = true;
		PAGE.closeInteractive();
		if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview("flash/detail-open.html");}
	};
	this.openInteractiveFromFlash = function(){
		PAGE.openInteractive();
		PAGE.interactiveEnable = false;
		if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview("flash/detail-close.html");}
	};
	this.tableRows = function() {
		$('#text_box tbody tr').each( function() {
				if ($(this).parents('table').hasClass('nodark')) return;
				if( !$(this).hasClass("dark") && !$(this).prev("tr").hasClass("dark")) {
					$(this).addClass("dark");
				}
				else {
					//$(this).addClass("dark");
				};
			}
		)
	}
	this.block = function() {
		$('#systemWorking').fadeIn();
	};
	this.unblock = function() {
		$('#systemWorking').fadeOut();
	};
	this.pagePath = window.location.pathname;
	//var pageName = pagePath.substring(pagePath.lastIndexOf('/') + 1);
	this.print = function(){
		window.print();
	};	
	
	this.fixIframe = function(height){
		//alert('a');
		//log($(el).find('div'));
		$('#text_box iframe').css({
			"height": height
		});
		$('#text_box iframe').attr('scrolling', 'no');
	};
	
};

var liscataOpen = function(url) {
	if (url == '') return false;
	if (jQuery.browser.msie) {
		window.location.replace(url);
		return true;
	}
	
	$('#inter_right').show();
	$('#inter_right .content').html('<center style="margin-top: 135px;"><img src="' + ROOT + 'themes/images/liscata_loading.gif"/></center>');
	if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview(url);}
	$.post(url, null, function(data, textStatus) {
		_body = $(data).find('#inter_right .obsah').html();
		$('#inter_right .obsah').html(_body);
		$.getScript(ROOT + 'js/sifr-config.js');
	});
	return false;
};

var liscataHook = function(){
	if (HTMLinfo.page_name == 'liscata') {
	
		$('#fotoalbum .item .img').css({
			'cursor': 'pointer'
		}).unbind().bind('click', function(){
			_class = $(this).children().attr('class');
			rot = _class.substr(1);
			_rel = $(this).children().attr('rel');
			_rel = _rel.split('|');
			openImage($(this).children().attr('src'), rot, _rel[2], _rel[3], _rel[0], _rel[1]);
		});
	
	
		$('#promo ul li, #promo ul li h2, #promo ul li h2 a, #promo ul li h2 object, #promo ul li a').unbind('click');
		$('#promo ul li h2, #promo ul li h2 object').css({cursor: 'pointer'});
		$('#promo ul li a, #inter_right .index a.next, #liscata_left a.more, .pager .cntr li a, .pager .prev a, .pager .next a').unbind().bind('click', function(){
			var _href = $(this).attr('href');
			if($(this).attr('target') != '_blank') {
				liscataOpen(_href);
			} else {
				 window.location.replace(_href);
			};
			return false;
		});
		$('#promo ul li').bind('click', function(){
			var _href = $(this).find('a:first').attr('href');
			odkaz = $(this).find('a:first');
			if(odkaz.attr('target') != '_blank') {
				liscataOpen(_href);		
			} else {
				 window.location.replace(_href);
			};
			return false;
		});
		
		$('#inter_right .upload').upload({
			name: 'file',
			method: 'post',
			enctype: 'multipart/form-data',
			action: $('#nahrat').attr('action'),
			onSubmit: function() {
				$('#nahrat .left div.status').text('Nahrávám fotografii...');
			},
			onComplete: function(data) {
				eval(data);
			}
		});
	};
}

var PAGE = new cPage();

$(function(){
	PAGE.init();
	MENU.init();
	
	$(".submenu a").each( function(){
		if (PAGE.pagePath == $(this).attr("href") ){
			$(this).addClass("active");
		};
	});
	
	$('#advisor_box form').bind("submit", function(){
		var url = $(this).attr('action');
		if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview(url + $('#advisor_box .advisor_search').val());}
		$.facebox(function() {
			pars = $('#advisor_box form').serialize();
			$.post(url, pars, function(data, textStatus) {
				_body = $(data).find('#zastupci_box').html();
				PAGE.unblock();
				$.facebox(_body);
				if (typeof pageTracker == 'undefined') {
					$('#myIFrame').attr('src', 'http://www.cmss-oz.cz/' + $('#myIFrame').attr('rel'));
				} else {
					$('#myIFrame').attr('src', pageTracker._getLinkerUrl('http://www.cmss-oz.cz/' + $('#myIFrame').attr('rel')));
				}
			});
		});		
		return false;
	});

	
	$('a[rel=facebox]').bind('click', function(){
		url = $(this).attr('href');
		if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview(url);}
		$.post(url, null, function(data, textStatus) {
			_body = $(data).find('#text_box').html();
			$.facebox(_body);
		});
		return false;
	}); 
	$('a[rel^=facebox-image]').bind('click', function(){
/*		url = $(this).attr('href');
		data = '<img src="' + url + '"/>';
		$.facebox(data);
		return false;
*/
		url = $(this).attr('href');
		if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview(url);}
		_rel = $(this).attr('rel');
		var tmp = _rel.split('-');
		if (typeof tmp[2] != 'undefined' && typeof tmp[3] != 'undefined') {
			data = '<img src="' + url + '" width="' + tmp[2] + '" height="'+tmp[3]+'"/>';
		} else {
			data = '<img src="' + url + '"/>';
		}
		$.facebox(data);
		return false;

	}); 

	$('a[rel^=facebox-int]').bind('click', function(){
		url = $(this).attr('href');
		if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview(url);}
		_rel = $(this).attr('rel');
		var tmp = _rel.split('-');
		if (typeof tmp[2] != 'undefined' && typeof tmp[3] != 'undefined') {
			$.post(url, null, function(data, textStatus) {
				_body = $(data).find('#text_box').html();
				$.facebox('<div style="width: '+tmp[2]+'px; height: '+tmp[3]+'px; padding:10px;">' + _body +'</div>');
				$.getScript(ROOT + 'js/sifr-config.js');
			});
		} else {
			$.post(url, null, function(data, textStatus) {
				_body = $(data).find('#text_box').html();
				$.facebox(_body);
				$.getScript(ROOT + 'js/sifr-config.js');
			});
		}
		return false;
	});
	
	
	$('a[rel^=facebox-ext]').bind('click', function(){
		url = $(this).attr('href');
		if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview(url);}
		_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;
	});
	
	$('a.calc').unbind('click');
	$('a.calc').bind('click', function(){
	url = $(this).attr("href");		
	if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview(url);}
	jQuery.facebox(function() { 
			 $.plugin('calcs',{
						files: [ROOT + 'modules/kalkulacka-pf/index.css', ROOT + 'modules/kalkulacka/inflace-najmu.js', ROOT + 'modules/kalkulacka/stavebni-sporeni.js', ROOT + 'modules/kalkulacka/uspory-vynosy.js', ROOT + 'modules/kalkulacka/uspory-zamestnance.js', ROOT + 'modules/kalkulacka/uspory-zamestnavatele.js', ROOT + 'modules/kalkulacka-rzp/index.js', ROOT + 'modules/kalkulacka-pf/index.js'],
						selectors:	['body'],
						cache:		false
					});
					$.plugin('calcs', function(){
							$.post(url, null, function(data, textStatus) {
							_body = '<div style="width:700px;">' + $(data).find('#text_box').html() + '</div>';
							$.facebox(_body);
							setTimeout(function(){
								$('.facebox_text iframe').height(400);
								$('.facebox_text iframe, #facebox iframe').attr('scrolling', 'auto').css({'overflow':'scroll'});
							}, 10);
							setTimeout(function(){
								$('.facebox_text iframe').height(400);
								$('.facebox_text iframe, #facebox iframe').attr('scrolling', 'auto').css({'overflow':'scroll'});;
								
							}, 500);
							$.getScript(ROOT + 'js/sifr-config.js');
						});
			
					});
		});
		return false;
	});

	$('a.inv_dot').unbind('click');
	$('a.inv_dot').bind('click', function(){
			url = $(this).attr("href");		
			if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview(url);}
			jQuery.facebox(function() { 
					 		$.plugin('investicni-dotaznik',{
								files: [ROOT + 'modules/investicni-dotaznik/index.js'],
								selectors:	['body'],
								cache:		false
							});
							$.plugin('investicni-dotaznik', function(){
									$.post(url, null, function(data, textStatus) {
										_body = '<div id="text_box" style="width:700px;">' + $(data).find('#text_box').html() + '</div>';
										$.facebox(_body);
											var q1 = 0;
											var q2 = 0;
											var q3 = 0;
											var q4 = 0;
											$("input[type='radio']").click( function(){
												if( $(this).attr("name") == "q1" ) {
													q1 = parseInt($(this).val());
												};
												if( $(this).attr("name") == "q2" ) {
													q2 = parseInt($(this).val());
												};
												if( $(this).attr("name") == "q3" ) {
													q3 = parseInt($(this).val());
												};
												if( $(this).attr("name") == "q4" ) {
													q4 = parseInt($(this).val());
												};
												suma = q1+q2+q3+q4;
												$("#pocet_bodu input").val(suma);
											});
									});					
							});
			});	
			return false;
	});
	$('a.sazebnik, a.law').unbind('click');
	$('a.sazebnik, a.law').bind('click', function(){
			PAGE.tmpurl = $(this).attr('href');
			if (typeof pageTracker == 'undefined') {} else {pageTracker._trackPageview(PAGE.tmpurl);}
			$.facebox( function(){ 
				$.post(PAGE.tmpurl, null, function(data, textStatus) {
					_body = $(data).find('#text_box').html();
					$.facebox('<div style="width:700px; height:400px; overflow-y:auto;" class="facebox_text">' + _body + '<br style="clear: both; height: 1px; line-height: 1px;"/></div>');
					setTimeout(function(){
						$('.facebox_text iframe').height(400);
						$('.facebox_text iframe').attr('scrolling', 'auto');
					}, 10);
					$.getScript(ROOT + 'js/sifr-config.js');
  			   });
				
			});
			return false;
	});
});

function zobrazSkryj(idecko){
		el=document.getElementById(idecko).style; 
		el.display=(el.display == 'block')?'none':'block';
};
