﻿(function($) {
 	$(document).ready(function() {
		init();
		if (!!Sys.WebForms.PageRequestManager.getInstance())
			Sys.WebForms.PageRequestManager.getInstance().add_endRequest(init);
			Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(save);
	});
	
	var izborTrake;
	var izborCustomLastrine;
	var izborLastrine;
	
	function init() {
		// init fancybox
		$("a.fancybox").fancybox({
			'titleShow' : true,
			'titlePosition' : 'inside',
			'hideOnContentClick' : true,
			'overlayOpacity' : 0.4,
			'overlayColor' : '#000'
		});
		
		// init stepcarousel
		izborTrake = initGallery("mygalleryTrake", "galleryTrakePrev", "galleryTrakeNext");
		izborCustomLastrine = initGallery("mygalleryLastrineC", "galleryLastrineCPrev", "galleryLastrineCNext");
		izborLastrine = initGallery("mygalleryLastrine", "galleryLastrinePrev", "galleryLastrineNext");
		izborFigure = initGallery("mygalleryFigure", "galleryFigurePrev", "galleryFigureNext");
		
	}
	
	function initGallery(galleryId, prevId, nextId) {
		var gallery = $("#" + galleryId).moodular({
			auto: false,
			api: true,
			startOn: get_cookie(galleryId),
			continuous: false,
			speed: 700,
			scroll: 5
			
		});
		
		$("#" + nextId).bind("click", function() {
			gallery.next();
			return false;
		});
		$("#" + prevId).bind("click", function() {
			gallery.prev();
			return false;
		});
		
		return gallery;
	}
	
	// sprema cookie na početku ajax requesta
	function save() {
		if (!!izborTrake)
			document.cookie = 'mygalleryTrake=' + izborTrake.getCurrent();
		if (!!izborCustomLastrine)
			document.cookie = 'mygalleryLastrineC=' + izborCustomLastrine.getCurrent();
		if (!!izborLastrine)
			document.cookie = 'mygalleryLastrine=' + izborLastrine.getCurrent();
		if (!!izborFigure)
			document.cookie = 'mygalleryFigure=' + izborFigure.getCurrent();
	}
	
	function get_cookie ( cookie_name )
	{
		var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

		if ( results )
			return ( unescape ( results[2] ) );
		else
			return null;
	}
})(jQuery);
