/* Web Projects Common System v. 1.0. */
/* COMMON PROJECT Javscript functions */
function popUp(URL) { day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=620,height=620');"); }


$(function() {

   // table strange behaviour
	var $arr = $(".arrangement");
	if ($arr.length  > 0) {
		var $tbls = $arr.find("table");
		try {
			$tbls.each(function(){
				if ($(this).attr("align") == "left") {
					$(this).attr("align",null);
				}
			});
		} catch (e) {
			
			
		}
	}

	  ///////////////////////////
	 // server data behaviour //
	///////////////////////////
	if (typeof SERVER_DATA == 'undefined') SERVER_DATA = {};
	
	// INVALID LOGIN CREDENTIALS
	if (SERVER_DATA['invalid_login_credentials']) {
		
		$('.login').overlay("show");
		$(".invalid_login_credentials").fadeIn(500,function() {
			$('.login input:visible:first').select();
		}).html(SERVER_DATA['login_message']);
		
		setTimeout(function() {
			$('.login').find(".invalid_login_credentials").slideUp(500);
		},5000);
		
	}
	
	// SUBSCRIBER LOGGED OUT
	if (SERVER_DATA['subscriber_logged_out']) {
		$('.message').showSuccess(SERVER_DATA['logout_message']);
	}

	// SUBSCRIBER LOGGED IN BY FORM
	if (SERVER_DATA['subscriber_loggedin_by_form']) {
		$('.subscriber_options').hide().show(500);
	}
	
	// SUBSCRIBER LOGIN FAILED
	if (SERVER_DATA['login_failed']) {
		$('.message').showSuccess(SERVER_DATA['login_message']);
	}
	
	
	// REGISTRATION CONFIRMED
	if (SERVER_DATA['registration_confirmed']) {
		$('.message').showSuccess(SERVER_DATA['registration_message']);
	}
	
	// REGISTRATION CONFIRMATION FAILED
	if (SERVER_DATA['registration_confirmation_failed']) {
		$('.message').showError(SERVER_DATA['registration_message']);
	}


	////////////////
	///////////////
	
	// hide the message overlay when attempting to login
	$('.loginlink').bind("click",function() {
		$('.message').overlay("hide");
	});

	var animDuration = 300;	
	
	
	// submenu behaviour
	$("#category-navigation li a.catsubagent").click(function() {
		id = '#'+$(this).attr("rel");
		$submenu = $(id).children(); //.children();
		
		
		var $submenu = $(id);
		var $ul = $submenu.find("ul:first");
		var $li = $ul.children();
		
		
		if ($submenu.hasClass("hidden")) {
			$li.hide();
			$submenu.show();
			$(this).addClass("strong");
			$li.slideDown(animDuration,function() {
				$(this).show();
				$submenu.removeClass("hidden");
			});
		} else {
			$(this).removeClass("strong");
			$li.slideUp(animDuration,function() {
				$(this).hide()
				$submenu.addClass("hidden");
			});
		}
		$(this).blur();
	});

        // autoexpand submenu feature


$(".autoexpand1").each(function() {

	var $catsubagent = $(this).find("a:first");

	var id = $catsubagent.attr("rel");

	if (id != "") {

		var $category_submenu = $(document.getElementById(id));

		$catsubagent.addClass("strong");
		$category_submenu.removeClass("hidden").show();
	}
			

});

	// main slide show
	$(".slideShow").sliderotator({
		slideShow:true,
		prevAgent:'.slideNavigation .left:first',
		statusAgent: '.slideNavigation .middle:first',
		nextAgent:'.slideNavigation .right:first'
	}); // todo
	
	$(".slideDescriptionBG")
		.css({opacity:0.4})
	;
	
	$(".slideShow")
		.mouseover(function(){
			$(this).find(".slideDescriptionBG:visible:first").stop().fadeTo(300,0.7,function() {
				$(".slideDescriptionBG").css({opacity:0.7})
			});
		})
		.mouseout(function() {
			$(this).find(".slideDescriptionBG:visible:first").stop().fadeTo(300,0.4,function() {
				$(".slideDescriptionBG").css({opacity:0.4})
			});
		});
	
	// sidebar rotating	
	$(".rotating").each(function() {
		$(this).find("li:odd").addClass("odd");
		$(this).find("li:even").addClass("even");
	
		/*
		// Fix the height of the listRotators //
		var totalHeight = 0; 
		var numElements = 0;
		$(this).find("li").each(function(){
			totalHeight += $(this).height();
			numElements++;
		}); 
		var avgHeight = totalHeight / numElements;
		var minElementCount =  $(this).find("li:lt(7)").length;
		var newHeight = avgHeight * (minElementCount+1);
		$(this).parent().height( newHeight );  
		*/
		
	});
	
	
	// broj elemenata u listi
	var numListItems = (typeof SERVER_DATA['num_list_items'] != 'undefined') ?  SERVER_DATA['num_list_items'] : 5 ;
	var listHeight = numListItems * 58;
	
	$(".rotating").listrotator({
		displayItems:numListItems,
		prevAgent:'.arrow-navigation .left',
		nextAgent:'.arrow-navigation .right',
		posAgent:'.arrow-navigation .middle label',
		autoRotate:4000,
		betweenEachDelay:5000
	});
	
	$("#sidebar .section").height(listHeight);
	
	
	
	$(".rotating").each(function() {
		
	});
	
	
	 // Site Message overlay
	$('.message.overlay').overlay({
		useCurtain:false
	});
	$('#message_close_link').click(function() {
		$(this).parent().overlay("hide");
		if ($(this).parent().hasClass("success")) {
			$(".reservation").overlay("hide");
			$(".registration").overlay("hide");
		}
	});
	//
	
	
	// reservation overlay
	$('.reservation').overlay({
		animDuration:600,
		useCurtain:false,
		triggerElement:'a#reserve',
		curtainBackground:'black',
		opacity:0.2
		/*show:true*/
	});
	
	// registration overlay
	$('.registration').overlay({
		animDuration:400,
		useCurtain:false,
		triggerElement:'a#register',		
		closeElement:'a#registration_hide'
	});
	
	// login overlay
	$('.login').overlay({
		animDuration:400,
		useCurtain:false,
		triggerElement:'a.loginlink',		
		closeElement:'a#login_hide'	
	});

	
	
	// switches and panels
	var animDuration = 300;
	$('.switches').each(function() {
		$(this).find('.switch').click(function() {
			
			if ($(this).parent().hasClass("switchbox")) {
				$(this).parent().addClass("current").siblings().removeClass("current");
			}
			
			
			$(".panel#"+$(this).val()).stop().fadeIn(animDuration,function() {
				$(this).find("input:first").select().focus();
			}).siblings().hide();
			
		});
	});
	
	 /////////////////////////
	// SEARCH AUTOCOMPLETE //
   /////////////////////////
   
	$("[name=search]").autocomplete(
		"modules/search/autocomplete.php",
		{
			selectFirst:false,
			delay:300,
			width:240,
			onItemSelect:function() {
				$("[name=search]:first").findForm().submit();
			}
		}
	);
	
	
	/* SEARCH HINT */
	$("#search-hint").click(function() {
		$(this).parent().find("[name=search]:first").focus();
		$(this).hide();
	});
	
	$("[name=search]:first")
		.focus(function() {
			$("#search-hint").hide();
		})
		.blur(function() {
			if ($(this).val()=='') $("#search-hint").show();
		});
	
	if ($("[name=search]:first").val()!='') {
		$("#search-hint").hide();
	}
	
	
	// remove site meter if not admin
	if (typeof SERVER_DATA['admin_loggedin'] == 'undefined') {
		$("#idSiteMeterHREF").remove();
	}
	
	
});
