jQuery(document).ready(function() {

	/*
	// jQuery Events Debugging
	$.fn.listHandlers = function(events, outputFunction) {
	    return this.each(function(i){
	        var elem = this,
	            dEvents = $(this).data('events');
	        if (!dEvents) {return;}
	        $.each(dEvents, function(name, handler){
	            if((new RegExp('^(' + (events === '*' ? '.+' : events.replace(',','|').replace(/^on/i,'')) + ')$' ,'i')).test(name)) {
	               $.each(handler, function(i,handler){
	                   outputFunction(elem, '\n' + i + ': [' + name + '] : ' + handler );
	               });
	           }
	        });
	    });
	};
	*/

	var urlDirFull = jQuery.url.attr("directory");

	/* --- jQuery extensions --- */
	
	// css properties selectors
	jQuery.extend(jQuery.expr[':'],{
		float: function(a) {
		  return (jQuery(a).css("float") === "left" || jQuery(a).css("float") === "right");
		},
		inline: function(a) {
		  return jQuery(a).css("display") === "inline";
		},
		marginx: function(a) {
		  return ((parseInt(jQuery(a).css("margin-left")) > 0) || (parseInt(jQuery(a).css("margin-right")) > 0));
		},
		marginy: function(a) {
		  return ((parseInt(jQuery(a).css("margin-bottom")) > 0) || (parseInt(jQuery(a).css("margin-top")) > 0));
		},
		margin: function(a) {
		  return ((parseInt(jQuery(a).css("margin-left")) > 0) || (parseInt(jQuery(a).css("margin-right")) > 0) || (parseInt(jQuery(a).css("margin-bottom")) > 0) || (parseInt(jQuery(a).css("margin-top")) > 0));
		}
	});
	

	/* --- bookmark & set as homepage --- */

	jQuery("#s-m-fav").click(function() {
		arctBookmarkIt('Naslov priljubka', 'http://www.spletna-stran.si');
	});
	jQuery("#set-as-homepage-button").click(function() {
		arctSetAsHomepage(this, 'http://www.spletna-stran.si');
	});


	/*--- printing ---*/

	jQuery(".print").click(function(e) {
		e.preventDefault();
		window.print();
	});
	
	
	/*--- poll ---*/
	
	jQuery("form#poll-form").submit(function() {
		var responseData = '';
		if (jQuery('#multi-answers').val() != '1') 
			// one answer
			responseData = jQuery("form#poll-form input:checked").val();
		else {
			// multiple answers
			jQuery("form#poll-form input:checked").each(function () {
				responseData += this.value + ' ';
			});
		}
		jQuery.post("/util/ajaxresponse.php",{
				func: "poll", 
				response: responseData
		     }, function(html) {
		   showPoll(html);
		 });
		return false;
	});

	function showPoll(htmlResponse) {
		jQuery("#poll-container").html(htmlResponse);
		jQuery("#poll-container img").each(function() {
			var tmp = jQuery(this).attr("class");
			var aVals = tmp.match(/[0-9]+$/); // get number at the end of the string
			var val = aVals[0];
			jQuery(this).animate({ width: val+"px" }, 1000 );
		});
	}
	
	
	/*--- FAQ ---*/
	
	if ($("div#faq-form-section").length) {
		$("p#faq-form-toggle").slideDown();
		$("p#faq-form-toggle").click(function() {
			$("p#faq-form-toggle").slideUp(500);
			$("div#faq-form-section").slideToggle(500);
		});
	}
	
	if ($("div#faq-list").length) {
		$("div#faq-list h2").click(function() {
			$(this).next("dl.faq-list").slideToggle(500);
			$(this).next("dl.faq-list").toggleClass('open');
			$(this).toggleClass('open');
		});
	}


	/* --- text resizing --- */

	jQuery("#t-s-normal").click(function(e) {
		jQuery("body").css({"fontSize": "62.5%"});
		jQuery.cookie('page_font_size', "62.5%", {path: '/'}); // save font size in cookie
	})

	jQuery("#t-s-larger").click(function(e) {
		jQuery("body").css({"fontSize": "70%"});
		jQuery.cookie('page_font_size', "70%", {path: '/'}); // save font size in cookie
	})

	jQuery("#t-s-largest").click(function(e) {
		jQuery("body").css({"fontSize": "85%"});
		jQuery.cookie('page_font_size', "85%", {path: '/'}); // save font size in cookie
	})

	// retrieve font size if cookie has been set
	var cookieFontSize = jQuery.cookie('page_font_size');
	if (cookieFontSize != '') {
		jQuery("body").css({"fontSize": cookieFontSize});
	}


	/* --- alt style switcher --- */
	
	function switchStylestyle(styleName) {
		$('link[@rel*=style][title]').each(function(i) {
        	this.disabled = true;
	        if (this.getAttribute('title') == styleName) this.disabled = false;
	    });
		$.cookie('style', styleName, {path: '/'});
	}
	
	function clearStylestyle() {
		$('link[@rel*=style][title]').each(function(i) {
        	this.disabled = true;
	    });
		$.cookie('style', null, {path: '/'});
	}
	
	$('#s-m-s-contrast').click(function() {
		var c = $.cookie('style');
		if (c) {
			clearStylestyle();
		} else {
    		switchStylestyle("high contrast style");
		}
    	return false;
	});
	var c = $.cookie('style');
	if (c) switchStylestyle(c);
	
	
	/* --- anchor click scrolling --- */
	
	$('a[href^=#]').click(function(e) {
		// scroll to anchor if there is actually more after the # sign in the link
		if ($(this).attr('href').length > 1) {
			scrollToElementName = $(this).attr('href').substr($(this).attr('href').indexOf("#")+1);
			scrollTo = $("a[name="+scrollToElementName+"]").offset().top;
			$('html, body').animate({scrollTop: scrollTo}, 500);
			e.preventDefault();
		}
	});	


	/* --- populate text fields and clear them on focus --- */

	jQuery.fn.textBoxHint = function () {
		return this.each(function () {
	    	var t = jQuery(this); // get jQuery version of 'this'
			var title = t.attr('title'); // get it once since it won't change
		    // only apply logic if the element has the attribute
		    if (title) {
				// on blur, set value to title attr if text is blank
				t.blur(function () {
					if (t.val() == '') {
						t.val(title);
						t.addClass('blur');
					}
				});
				// on focus, set value to blank if current value
				// matches title attr
				t.focus(function () {
					if (t.val() == title) {
						t.val('');
						t.removeClass('blur');
					}
				});
				// clear the pre-defined text when form is submitted
				t.parents('form:first').submit(function() {
					if (t.val() == title) {
						t.val('');
						t.removeClass('blur');
					}
				});
				t.blur(); // now change all inputs to title
			}
		});
	}


	/* --- table zebra striping --- */

	jQuery.fn.zebraStripeIt = function() {
		jQuery("tr:odd", this).addClass("odd"); // set class to every odd row in a table
	}


	/* --- external links --- */

	$.fn.markExternalLinks = function() {
		$(this).filter(function() {
			return this.hostname && this.hostname !== location.hostname;
		}).addClass("external");
	}

	/* --- Photogallery --- */

	function initPhotogallery() {
		if (jQuery("#photo-gallery-container").length) {
			var galleryWidth = jQuery("div#photo-gallery-container").width();
			var galleryItemWidth = jQuery("div#photo-gallery-container .photo-gallery-item:first").width();
			var itemsPerRow = Math.floor(galleryWidth / galleryItemWidth);
			var cnt = 1;
			var cntItems = 0;
			var maxHeight = 0;
			var aHighest = new Array();
			var nItems = jQuery("div#photo-gallery-container .photo-gallery-item").length;
			jQuery("div#photo-gallery-container .photo-gallery-item").each(function() {
				cntItems++;
				if (cnt > itemsPerRow) {
					cnt = 0;
					maxHeight = 0;
				}
				//var totalHeight = 
				if (jQuery(this).height() > maxHeight) {
					maxHeight = jQuery(this).height();
				}
				if (cnt == itemsPerRow || cntItems == nItems) {
					aHighest.push(maxHeight);
				}
				cnt++;
			});
			cnt = 1;
			cnt2 = 0;
			jQuery("div#photo-gallery-container .photo-gallery-item").each(function() {
				if (cnt > itemsPerRow) {
					cnt = 1;
					cnt2++;
				}
				jQuery(this).height(aHighest[cnt2]);
				
				// check for thumbs which are too wide and remove the height attribute
				if (jQuery(this).width() <= jQuery("img", this).width()) {
					jQuery("img", this).removeAttr("height");
				}
				
				cnt++;
			});
		}
	}

	
	/* --- Photolist --- */
	
	// resize the photolist widths for non-standard width photos
	function resizePhotolist() {
		if (jQuery("dl.photo-list").length) {
			var imgWidth = jQuery("dl.photo-list img:first").width();
			var dlWidth = jQuery("dl.photo-list").width();
			
			jQuery("dl.photo-list dt, dl.photo-list dd.summary").width(dlWidth - imgWidth - 10 + "px");
		}
	}

	
	$('input:text').textBoxHint(); // titles to input text
	$(".table-data").zebraStripeIt(); // alternate coloring of table rows

	

	$(".language-open").click(function(){
		if ($("ul#language-select").css('display')=='block') {
			$("ul#language-select").css('display','none');
		} else { 
			$("ul#language-select").css('display','block');
		}
	});
	
	$(window).load(function() {
		resizeFooter();	
		resizeGallery();			
	});	

	/*
	$("#main-menu li.menu").mouseover(function() {
		$(this).next('li.subtitle').css('display','block');
	});
	
	$("#main-menu li.menu").mouseout(function() {
		$(this).next('li.subtitle').css('display','none');
	});	
	*/

	/* --- window resizing --- */
	
	function resizeGallery() {
		browser = $(window).width();
	
		if (browser >800) {
			width = 733;
			galleryWidth = browser-733;
		
			height = $("#content").height()+89;
		
			$("#ambient-container").css("position","absolute");		
			$("#ambient-container").css("width", galleryWidth+"px");
			$("#gallery").css("width", galleryWidth+"px");		
			$("#ambient-container").css("height", height+"px");		
		}
	}
	
	
	function resizeFooter() {
		browser = $(window).width();
		topWidth = browser;
		
		if (browser < 1137 ) {
		
			if (browser <1006) browser = 1006;
		
			margin = $("#fix").css("margin-left");
			margin = parseInt(margin.replace("px",""));
			padding = $("#footer-row1-col2").css("padding-left");			
			padding = parseInt(padding.replace("px",""));		
			width = $("#footer-row1-col2").css("width");
			width = parseInt(width.replace("px",""));				
			padding1 = $("#footer-row1-col1").css("padding-left");			
			padding1 = parseInt(padding1.replace("px",""));
			width1 = $("#footer-row1-col1").css("width");
			width1 = parseInt(width1.replace("px",""));

			while (((width + padding + width1 + padding1) > browser) && (margin>2 || width>559 || padding>10  || width1>365) ) {  
				
					if (width > 559) {
						width--;	
					}			
					
					if(margin > 2) {
						margin--;
					}
			
					if (padding > 10) {
						padding--;
					}				
				
					if(width1 > 365) {
						width1--;
					}				
				
				$("#footer-row1-col2").css("width",width+"px");			
				$("#footer-row1-col1").css("width",width1+"px");		
				$("#fix").css("margin-left",margin+"px");
				$("#footer-row1-col2").css("padding-left",padding+"px");					
			}			
		}else {
			if($(window).width()>=1137) {
				$("#fix").css("margin-left","81px");
				$("#footer-row1-col2").css("padding-left","39px");		
				$("#footer-row1-col2").css("width","630px");			
				$("#footer-row1-col1").css("width","388px");									
			}
		}	
	}
	
	$(window).resize(function() {
		resizeFooter();
		resizeGallery();
	});
	
	
	/*$("#map-area").mouseover(function(e) { 
		
		offset = $("#map-area").offset();
		relativeX = e.pageX - offset.left;
		relativeY = e.pageY - offset.top;
		mapXnew = -110-(relativeX-110);
		mapYnew = -350-(relativeY-350);			
		$("#big-map").css("left",mapXnew+"px");
		$("#big-map").css("top",mapYnew+"px");	
		
		$('#big-map').fadeIn(2000);
		$('#map').fadeOut(2000)
	});
		
	$("#big-map").mouseout(function() { 
		$('#big-map').stop(true, true).fadeOut(2000);
		$('#map').stop(true, true).fadeIn(2000);
	});*/
	
	$("#big-map").mouseout(function() { 
		$("#big-map").animate({
				left: "-75px",
				top: "-115px"
				},1000);
	});

	$("#big-map").mousemove(function(e){
		offset = $("#map-area").offset();
		relativeX = e.pageX - offset.left;
		relativeY = e.pageY - offset.top;
			
						
		mapXnew = -110-(relativeX-110);
		mapYnew = -350-((Math.round(3*relativeY))-350);
		if ((relativeX > 0 && relativeX< 220) && (relativeY > 0 && relativeY< 175)) {
			$("#big-map").animate({
				left: mapXnew+"px",
				top: mapYnew+"px"
				},0);
		}
	});	
	


	// List all handlers for all events of all elements:
	//$('*').listHandlers('*', console.info);

});
