$(document).ready(function() {

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

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

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

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

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


	/*--- printing ---*/

	$("#print").click(function(e) {
		e.preventDefault();
		window.print();
	});

	/*--- END printing ---*/
	
	
	/*--- menu ---*/
/*
	$("#main-menu ul").each(function() {
		var ulClass = $(this).attr("class");
		$("li:first-child", this).addClass(ulClass+'-first');
		$("li:last-child", this).addClass(ulClass+'-last');
	});
*/
	/*--- END menu ---*/
	
	
	/*--- poll ---*/
	
	$("form#poll-form").submit(function() {
		var responseData = '';
		if ($('#multi-answers').val() != '1') 
			// one answer
			responseData = $("form#poll-form input:checked").val();
		else {
			// multiple answers
			$("form#poll-form input:checked").each(function () {
				responseData += this.value + ' ';
			});
		}
		$.post("/util/ajaxresponse.php",{
				func: "poll", 
				response: responseData
		     }, function(html) {
		   showPoll(html);
		 });
		return false;
	});

	function showPoll(htmlResponse) {
		$("#poll-container").html(htmlResponse);
		$("#poll-container img").each(function() {
			var tmp = $(this).attr("class");
			var aVals = tmp.match(/[0-9]+$/); // get number at the end of the string
			var val = aVals[0];
			$(this).animate({ width: val+"px" }, 1000 );
		});
	}
	
	/*--- END poll ---*/
	
	
	/*--- 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');
		});
	}
	
	/*--- END FAQ ---*/


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

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

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

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

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

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


	/* --- 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);

	/* --- END alt style switcher --- */
	


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

	$.fn.textBoxHint = function () {
		return this.each(function () {
	    	var t = $(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
			}
		});
	}

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


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

	$.fn.zebraStripeItEven = function() {
		$("> *:even", this).addClass("even"); // set class to every even child inside the given parent
	}
	
	$.fn.zebraStripeItOdd = function() {
		$("> *:odd", this).addClass("odd"); // set class to every odd child inside the given parent
	}
	
	$.fn.zebraStripeTable = function() {
		$("tr:odd", this).addClass("odd"); // set class to every odd child inside the given parent
	}

	/* --- END zebra striping --- */


	/* --- data table cell linking --- */

	/*
	 * Assigns a clickable and linked area to the whole table row (or individual cell if in the head).
	 * The url is taken from the first 'a' element inside the current row.
	 */
	$.fn.linkTableCells = function() {
		// body cells (rows)
		$("tbody tr", this).each(function() {
			var link = $("a[href != '#']", this).eq(0).attr("href"); // pick first valid link from all a elements in the current row
			$("td, a", this).click(function(e) {
				// prevent jumping to the top of the page for invalid (#) links
				if (link == undefined) {
					e.preventDefault();
				} else {
					// see if there is an input inside the td
					if ($("input", this).length) {
						
					// see if link is not an attachment
					} else if (link.search(/mma_bin/) == -1) {
						window.location.href = link;
					}
				}
			});
		});
		// head cells (usually used for sorting)
		$("thead th a", this).each(function() {
			$(this).parent("th").css("cursor", "pointer");
			// disabled in AJAX sorting
			/*$(this).parent("th").click(function() {
				window.location.href = $("a", this).attr("href");
			});*/
		});
	}

	/* --- END data table cell linking --- */


	/* --- image framer --- */

	$.fn.initImageFramer = function() {
		$(this).each(function() {
			var imageTitle = $(this).attr("title"); // get title text

			if (imageTitle == '') {
				imageTitle = $(this).attr("alt"); // no title text available, use alt text
			}
			// no text available - skip
			if (imageTitle != '') {
				$(this).wrap('<div id="photo-frame"></div>');
				$(this).after('<p>'+imageTitle+'</p>');
			}
		});
	}

	/* --- END image framer --- */


	/* --- calendar --- */

	function initCalendar() {
		// advance a month
		$("#cal-next-month").click(function(e) {
			var cName = $("#cal-next-month").attr("class");
			var y = cName.substr(6,4);
			var m = cName.substr(10,2);
			$("#calendar-response").load("/util/ajaxresponse.php", { 'func': 'outputCalendar', 'y': y, 'm': m}, function() {
				initCalendar();
			});
			e.preventDefault();
		});

		// backtrack a month
		$("#cal-prev-month").click(function(e) {
			var cName = $("#cal-prev-month").attr("class");
			var y = cName.substr(6,4);
			var m = cName.substr(10,2);
			$("#calendar-response").load("/util/ajaxresponse.php", { 'func': 'outputCalendar', 'y': y, 'm': m}, function() {
				initCalendar();
			});
			e.preventDefault();
		});
	}
	
	/* --- END calendar --- */
	
	
	/* --- anchor click scrolling --- */
	
	$('a[href^=#]').click(function(e) {
		scrollToElementName = $(this).attr('href').substr($(this).attr('href').indexOf("#")+1);
		scrollTo = $("a[name="+scrollToElementName+"]").offset().top;
		$('html, body').animate({scrollTop: scrollTo}, 500);
		e.preventDefault();
	});
	
	/* --- END anchor click scrolling --- */
	
	
	/* --- sysmenu --- */
	/*
	function moveSysmenu() {
		var version = getInternetExplorerVersion();
		if (version > -1 && version < 7.0) {
			$("#footer").css("top", $(window).scrollTop() + $(window).height() - 27 + "px");
		}
	}*/
	
	/* --- END sysmenu--- */
	
	
	/* --- seminars form --- */
	
	$("#seminars-form-open-link").click(function(e) {
		$("#form-seminar").slideToggle('slow');
		e.preventDefault();
	});
	
	// close second part of form if payee checkbox is checked
	if ($("#fSmnr13:checked").length == 0) {
		$("#seminars-form-part2").hide();
	}
	
	// toggle second part of form if payee checkbox has been checked/unchecked
	$("#fSmnr13").click(function() {
		if ($("#fSmnr13:checked").length) {
			$("#seminars-form-part2").slideDown('slow');
		} else {
			$("#seminars-form-part2").slideUp('slow');
		}		
	});
	
	// close third part of form if employment checkbox is checked
	if ($("#fSmnr8:checked").length == 0) {
		$("#seminars-form-part3").hide();
	}
	
	// toggle third part of form if employment checkbox has been checked/unchecked
	$("#fSmnr8").click(function() {
		if ($("#fSmnr8:checked").length) {
			$("#seminars-form-part3").slideDown('slow');
		} else {
			$("#seminars-form-part3").slideUp('slow');
		}		
	});
	
	/* --- END seminars form --- */
	
	/* --- seminars impruving form --- */
	
	
	// close second part of form if payee checkbox is checked
	
	if ($("#fSmnr11:checked").length == 0) {
		$("#eminars-form-part-improving").hide();
	}
	
	// toggle second part of form if payee checkbox has been checked/unchecked
	$("#fSmnr11").click(function() {
		if ($("#fSmnr11:checked").length) {
			$("#eminars-form-part-improving").slideDown('slow');
		} else {
			$("#eminars-form-part-improving").slideUp('slow');
		}		
	});
	
	/* --- END seminars form --- */
	
	
	/* --- 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++;
			});
		}
	}
	
	/* --- END Photogallery --- */
	

	/* --- on load --- */

	$('input:text').textBoxHint(); // titles to input text
	//$("#text-content-container table").zebraStripeTable(); // alternate coloring of table rows
	$(".table-data").linkTableCells(); // make datatable cells clickable (linked)
	initCalendar(); // initialize the calendar
	// leading images slideshow
	$('#lead-images').cycle({
		fx: 'fade',
		timeout: 3000,
		speed: 3500,
		random: 1
	});
	//$("#module-recent-events ul").zebraStripeItEven(); // alternate coloring of li elements

	/* wait for images to load too */
	$(window).load(function() {
		$("#text-content-container img.content-photo").initImageFramer(); // format content images with title
		initPhotogallery(); // initialize the photogallery
	});
	
	/* --- END on load --- */


	/* --- window resizing --- */
	
	$(window).resize(function() {
		
	});
	
	/* --- END window resizing --- */
	
	
	/* --- document scrolling --- */
	
	$(window).scroll(function() {
		
	});
		
	/* --- END document scrolling --- */
	
	
	$("li.toggle-trigger span").click(function(e) {
		var parent = $(this).parent();
		var parentul = parent.parent();
		$("li.toggle-trigger").children("ul").not(parentul).slideUp('slow');
		$("> ul", parent).slideDown('slow');
		e.preventDefault();
	});
	
	
	/* --- cart ajax --- */
	
	$(".btnCart").click(function() {
		var idValue = $(this).attr("id");
		var id = idValue.substring(4);
		var quantity = $(this).prevAll("input[name=quantity]").val();
		var price = $(this).prevAll("input[name=price]").val();
		var data = {
			'articleId': id,
			'quantity': quantity,
			'price': price
		}
		jQuery($("#module-shopping-cart").parent(":first")).load("/util/ajaxresponse.php?act=addCart", data, function() {

		});
	});
	
	
	
	function cartUpdate(){
		
	}
	
	function cartUpdateAll(){
		$(".imgCartEdit").click(function(e) {
			
			var idValue = $(this).attr("id");
			var id = idValue.substring(4);
			var data = {
				'articleId': id
			}
			jQuery($("#text-content-container")).load("/util/ajaxresponse.php?act=removeCart", data, function() {
				cartUpdateAll();
			});
			
		});
		
		$("#refresh-cart").click(function(e) {
			var aValues = new Array();
			var i = 0;
			$("input.quantity").each(function(e) {
				var idValue = $(this).attr("id");
				var id = idValue.substring(4);
				aValues[i] = id+"-"+$(this).val();
				i++;
			});
			var data = {
				'aValues[]' : aValues
			}
			jQuery($("#text-content-container")).load("/util/ajaxresponse.php?act=updateCart", data, function() {
				cartUpdateAll();
			});
		
			
		});
	}
	

	cartUpdateAll();
	
	/* --- END cart ajax --- */

});