
$(document).ready(function(){

	// clickable
	$(".clickable").click(
	function()
	{
	    window.location = $(this).attr("url");
    	return false;
	});	

	$('.answer').hide();
	$('.question').click(function(){
		$(this).next().toggle();
	});


	$("#hl_nav li").click(function(){
	    $("#hl_nav li").removeClass('active');
	    var classN = ".hl_item." + $(this).attr("class");
	    $(this).addClass('active');
	    $(".hl_item.active").fadeOut(400);
	    $(".hl_item.active").removeClass('active');
		$(classN).addClass('active');
		$(classN).fadeIn(400);
	    return false;
	});
	
	function getActiveHL() {
		var temp = $('#hl-item-holder').children();
		for(var i = 0; i <= temp.size(); i++) {
			if($(temp[i]).hasClass('active')) {
				return temp[i];
			}
		}
	}
	function getActiveNav() {
		var temp = $('#hl_nav').children();
		for(var i = 0; i <= temp.size(); i++) {
			if($(temp[i]).hasClass('active')) {
				return temp[i];
			}
		}
	}
	
	function moveToNext() {
		var currentHL = getActiveHL();
		var allHLs = $(currentHL).parent().children();
		var sizeHL = $(allHLs).size();
		var currentNav = getActiveNav();
		var allNav = $(currentNav).parent().children();
		if($(currentHL).index() == sizeHL - 1) {
			$(currentHL).removeClass('active');
			$(currentNav).removeClass('active');
			$(currentHL).fadeOut(400);
			$(allHLs[0]).addClass('active');
			$(allNav[0]).addClass('active');
			$(allHLs[0]).fadeIn(400);
			return false;
			
		}
		$(currentHL).removeClass('active');
		$(currentNav).removeClass('active');
		$(currentHL).fadeOut(400);
		$(currentHL).next().addClass('active');
		$(currentNav).next().addClass('active');
		$(currentHL).next().fadeIn(400);
		return false;
	}
	
	var rotate = setInterval(moveToNext, 8000);
	
	setTimeout(function() { $("#slider").fadeIn(800); }, 800);	
		
	// var temp = $(".hl-item-holder").children();
	// var count = temp.size();	
	
	// rollovery
	$(".rollover").hover(
	 function()
	 {
	  this.src = this.src.replace("_off","_on");
	 },
	 function()
	 {
	  this.src = this.src.replace("_on","_off");
	 }
	);
	
	// galeria
	// $("div.descr a.next").click(function(){
	// 	
	// 	var currItemID = "item_"+getPic(parseInt(this.id.split("_")[1]));
	// 	
	// 	$(".gal.active").fadeOut(400).removeClass('active');
	// 	$(".gal."+currItemID).addClass('active').fadeIn(400);
	// 	
	//     return false; 
	// });	
	// $("div.descr a.prev").click(function(){
	// 	
	// 	var currItemID = "item_"+getPic(parseInt(this.id.split("_")[1]));
	// 	
	// 	$(".gal.active").fadeOut(400).removeClass('active');
	// 	$(".gal."+currItemID).addClass('active').fadeIn(400);
	// 	
	//     return false;
	// });
	// news
	$(".news-items a.next").click(function(){
		
		var currItemID = "nitem_"+getPic(parseInt(this.id.split("_")[1]));
		
		$(".news_container.active").fadeOut(400).removeClass('active');
		$(".news_container."+currItemID).addClass('active').fadeIn(400);
		
	    return false; 
	});
	$(".news-items a.prev").click(function(){
		
		var currItemID = "nitem_"+getPic(parseInt(this.id.split("_")[1]));
		
		$(".news_container.active").fadeOut(400).removeClass('active');
		$(".news_container."+currItemID).addClass('active').fadeIn(400);
		
	    return false; 
	});	
	// quotes
	$("#quotes a.next").click(function(){
		
		var currItemID = "qitem_"+getPic(parseInt(this.id.split("_")[1]));
		
		$(".quote.active").fadeOut(400).removeClass('active');
		$(".quote."+currItemID).addClass('active').fadeIn(400);
		
	    return false; 
	});
	$("#quotes a.prev").click(function(){
		
		var currItemID = "qitem_"+getPic(parseInt(this.id.split("_")[1]));
		
		$(".quote.active").fadeOut(400).removeClass('active');
		$(".quote."+currItemID).addClass('active').fadeIn(400);
		
	    return false; 
	});	
	// publication
	$("#publication a.next").click(function(){
		
		var currItemID = "pitem_"+getPic(parseInt(this.id.split("_")[1]));
		
		$(".pub.active").fadeOut(400).removeClass('active');
		$(".pub."+currItemID).addClass('active').fadeIn(400);
		
	    return false; 
	});
	$("#publication a.prev").click(function(){
		
		var currItemID = "pitem_"+getPic(parseInt(this.id.split("_")[1]));
		
		$(".pub.active").fadeOut(400).removeClass('active');
		$(".pub."+currItemID).addClass('active').fadeIn(400);
		
	    return false; 
	});	
	
	
	function getPic(n) {
	    var noOfItems = $("div#gallery").children('div.gal').length;
		currPic = n;
		if (currPic > noOfItems)
			currPic = 1;
		if (currPic < 1)
			currPic = noOfItems;
		return currPic;
	}
	
	// custom select boxes
	(function($){
	 $.fn.extend({

	 	customStyle : function(options) {
		  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
		  return this.each(function() {

				var currentSelected = $(this).find(':selected');
				$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
				var selectBoxSpan = $(this).next();
				var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
				var selectBoxSpanInner = selectBoxSpan.find(':first-child');
				selectBoxSpan.css({display:'inline-block'});
				selectBoxSpanInner.css({width:'180px', display:'inline-block'});
				var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
				$(this).height(selectBoxHeight).change(function(){
				selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				});

		  });
		  }
		}
	 });
	})(jQuery);
	$('.mySelectBoxClass').customStyle();
	
	
	
        // advanced search
	$('.adv_search_toggle, #advanced_close').click(function(event){
		//$('#advanced_search_box').slideToggle(400);
                $('#advanced_search_box').toggle();
                $('.adv_search_toggle').toggle();
		var que = $('#box_header_top input.form_query').val();
		$('input#adv_que').val(que);
		return false;
	});
	
	$('#gallery').cycle({
		prev: '#prev_gal',
		next: '#next_gal'
	});
	$('#footer-partners-list').cycle();

	// datepicker
	if ($( "#datepicker" ).length > 0){
	    $('#datepicker').datepicker( {
		    //beforeShowDay : loadEventsToCalendar,
		    //onChangeMonthYear : loadEventsToCalendar,
		    onChangeMonthYearComplete : loadEventsToCalendar
		    //create : loadEventsToCalendar
		    //changeMonth : false,
		    //changeYear : false,
		    //showButtonPanel : false
	       });
	}

});

var events = new Object();

function selectEventDays(year, month){
	//alert('select events: ' + year + ' m: ' + month);
	var id = '-'+month+'-'+year;
	var a_id = year+''+month;
	jQuery.each( events[a_id], function(i, val) {
		//alert(' e ' + val);
		$('.'+val+id+' a').addClass('news');
		$('.'+val+id+' a').click(function(event){
		    $('a.news').removeClass('active');
		    $('.'+val+id+' a').addClass('active');
		    if ($( "#bigcal" ).length > 0){
			loadCalendarResult( year, month, val )
			return false;
		    }else{
			window.location = "/kalendarz.html?y="+year+"&m="+month+"&d="+val;
			return false;
		    }
		});
	});
}

function loadEventsToCalendar( year, month ){
    //alert('load events: ' + year + ' m: ' + month);
	var a_id = year+''+month;
    if( events[a_id] ) {
        selectEventDays( year, month);
    } else {
        jQuery.get('/page/box/column_cal_req.ajax', 
					{year : year, month : month}, 
					function ( data ){
						//alert('cal_req: ' + data);
						events[a_id] = data;
						selectEventDays(  year, month );
					},
					'json'
				);
    }
}

function loadCalendarResult( year, month, day ){
    if ($( "#bigcal" ).length > 0){
        $('#listBlockCalendar').load('/page/box/list_block_calendar.ajax?y=' + year + '&m=' + month + '&d=' + day);
    }
}

