/**
*	Some js
*/


hs.graphicsDir = 'js/highslide/highslide/graphics/';
hs.outlineType = '';
hs.showCredits = 0;

/*
hs.transitions = ['expand', 'crossfade'];
hs.targetX = 'inner 0px'; 
hs.targetY = 'inner 0px';
*/

/* flasham */
hs.wrapperClassName = 'draggable-header';
//hs.allowSizeReduction = false;
//hs.preserveContent = false;



//Add the controlbar

if (hs.addSlideshow) hs.addSlideshow({
	// slideshowGroup: 'gallery',
	interval: 5000,
	repeat: false,
	useControls: true,
	overlayOptions: {
		opacity: .75,
		position: 'bottom center',
		hideOnMouseOut: true
	}
});



//The gallery props

function getParms(parms) {

	/* outlineType: 'rounded-white', */	
	
	var opt = {wrapperClassName: 'glossy-dark',
			   dimmingOpacity: 0.5,
			   transitions: ['expand', 'crossfade'],
			   targetX : 'inner 0px', 
			   targetY : 'inner 0px'
			  };

	jQuery.extend( opt, parms );
		
	return opt;
}




var aniTotal = 0;
var aniIndex = -1;
var aniDepth = -99;

function animate() {
	
	aniTotal = $("div.bg").length;
	
	if(aniIndex == -1) {
		aniIndex = checkIndex();
		aniIndex = (aniIndex == 3) ? aniIndex = 0 : aniIndex; 
	}
	
	
	var visible = (aniIndex <= aniTotal - 1) ? (aniIndex == 0) ? aniTotal - 1 : aniIndex - 1 : 0;
	
	
	$("div.bg").css("zIndex", aniDepth);
	$( $("div.bg").get(visible) ).css("zIndex", aniDepth + 1);	
	$( $("div.bg").get(aniIndex) ).css("zIndex", aniDepth + 2).hide();
	
	
	$($("div.bg").get(aniIndex)).fadeIn(3600, function() {
		
		aniIndex = (aniIndex < aniTotal - 1) ? aniIndex += 1 : 0;
		setTimeout("animate()", 36000);
		
	});	
}



function checkIndex() {

	var k = 0;
	var visible = 0;
	
	$("div.bg").each(function(i, el) {
		
		if( $(this).is(':visible') ) {
			visible = k + 1;
		}
		
		k++;	
	});
	
	return visible;
}






var rotor;

$(document).ready(function(){
	
	
	setTimeout("animate()", 36000);
	
	
	
	
	// bind toggle for cat button images

	$("div#topButtons img").hover(
		function(){
			if($(this).attr("src").indexOf("0") != -1) {
					var newSrc = $(this).attr("src").replace("0","1");
					$(this).attr("src" ,newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("1") != -1 && this.className != "active") {
				var newSrc = $(this).attr("src").replace("1","0");
				$(this).attr("src",newSrc);
			}
		}
	);

	
	
	
	// e-katalogs

	$("div.katalogs, div#galerija").hover(
		function(){
			if($("img", this).attr("src").indexOf("0") != -1) {
					var newSrc = $("img", this).attr("src").replace("0","1");
					$("img", this).attr("src" ,newSrc);
			}
		},
		function(){
			if($("img", this).attr("src").indexOf("1") != -1 && this.className != "active") {
				var newSrc = $("img", this).attr("src").replace("1","0");
				$("img", this).attr("src",newSrc);
			}
		}
	);	

	

	rollOver();
	
	
	$.historyInit(pageload);

	
	// bindojam side a
	accelerate();
	
	
	// popups
	
	popups();


	// rotor

	rotor = new Rotor();

});







function popups() {

	$('a[@rel^=lightbox]').each(function() {
		
		var size = $(this).attr("rel").match(/(\d){1,3}/g);
		
		if(!size[0] || !size[1])
			return;
		
		
		$(this).unbind("onclick");
		
		this.onclick = function() {
			
			var isSWF = $(this).attr("href").match(/(swf)$/);
			
			if(isSWF) {
				return hs.htmlExpand(this, { objectType: 'swf', 
											 width: size[0], 
											 height: size[1], 
											 objectWidth: size[0], 
											 objectHeight: size[1], 
											 outlineType : 'rounded-white',
											 allowSizeReduction : false,
											 wrapperClassName : 'draggable-header no-footer'});
				
			} else if ($(this).attr("href").match(/(jpg|gif|png|bmp)$/) ) {
				
				//$(this).attr("href", $(this).attr("href").replace(/^(\/alan\/)/, "") );

				//return hs.expand(this, getParms({}));
				
				return hs.expand(this, { dimmingOpacity: 0.5,
					 						 transitions: ['expand', 'crossfade'],
					 						 targetX : 'inner 0px', 
					 						 targetY : 'inner 0px'});				
					 						
				
				
			} else {
				
				var href = $(this).attr("href");
				var base = document.getElementsByTagName("base");
					base = (base) ? base[0].href : "";
				
				link = href.replace(base, "");
				
				//alert(href + "\n" + link + "\n" + base);

				if(!link.match(/^http/)) {
					link = "feed.php" + link + "&cmd=body";
				} else {
					link = href;
				}
				
				
				return hs.htmlExpand(this, { objectType: 'iframe',
											 src: link,
											 width: size[0],
											 objectWidth: size[0],
											 height: size[1],
											 objectHeight: size[1],
											 slideshowGroup : 123,
											 outlineType : 'rounded-white',
											 wrapperClassName : 'draggable-header no-footer'
											 //targetX : 'inner 0px', 
											 //targetY : 'inner 0px'
											});
	
			}
		};
	});

}









function openCatalog(el) {
	
	
	var a = $("a", el);
	
	window.open(a.attr("href"), a.attr("target"));
}







function rollOver() {
	
	$("ul.side > li").hover(function() {
		
		if(!$(this).hasClass("active")) {
			$(this).addClass("roll");
			$("a", this).addClass("roll");
		}
	}, 
	function (){
		if(!$(this).hasClass("active")) {
			$(this).removeClass("roll");
			$("a", this).removeClass("roll");
		}
	}
	
);	
	
}






function dileri(id, el) {
	
	$.getJSON("feed.php?cmd=dileri&id=" + id + "&town=" + el,
			function(data){
			
				$("#inner").html( data.inner ? data.inner : "" );	
				$("#inner > *").fadeTo('fast', 1);

			});	
	
}






function pageload(hash) {
	
	if(hash.match(/^%/))
		return;
	
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		//console.log(hash);
		doLoad(encodeURIComponent(hash));
	} else {
		// start page
		//doLoad(location.href);
	}
}






function accelerate() {
	return;
	$("UL.side a, div#topButtons a, a.newsTitle, a").each(function(n, el) {
		
		//$(el).unbind("click");
		
		$(el).click(function() {
			
			// var id = $(this).attr("href").match(/\d/g).join('');
			var id = encodeURIComponent($(this).attr("href"));

			hs.close();
			
			/*
			$.getJSON("feed.php?cmd=load&id=" + id,
				function(data){
				
					$("#topButtons").html( data.top ? data.top : "" );
					$("#side").html( data.side ? data.side : "" );
					$("#inner").html( data.inner ? data.inner : "" );
																				
					rollOver();
					accelerate();
					popups();
				});
			*/
			
			//doLoad(id);
			
			var base = document.getElementsByTagName("base");
			base = (base) ? base[0].href : "";
			
			var link = $(this).attr("href").replace(base, "");
			
			$.historyLoad(link);
			
			
			return false;
		})
		
		
		
		
	});
		
	
}



function doLoad(id) {
	
	$.getJSON("feed.php?cmd=load&id=" + id,
			function(data){
			
				$("#topButtons").html( data.top ? data.top : "" );
				$("#side").html( data.side ? data.side : "" );
				$("#inner").html( data.inner ? data.inner : "" );
																			
				rollOver();
				accelerate();
				popups();
			});	

}





var Rotor = function() {

	var _self	= this;
	var t		= null;


	this.timer = function() {

		var items	= $('div.rotor');		
		var curr	= $('div.rotor:visible');		

		var n	= items.index(curr) + 1;

		if(n > (items.length - 1)) {
			
			n = 0;
		}

		var next = $(items.get(n));
		var rw = $('div#rotor-wrap');


		curr.hide();
		next.fadeIn('slow');

	}


	// init

	if($('div.rotor').length > 1) {

		// timer
	
		_self.t = setInterval(_self.timer, 4000);

		$('div#rotor-wrap').click(function() {

			_self.timer()
		});


		// stop on hover

		$('div#rotor-wrap').hover(function() {
		
			clearInterval(_self.t);
	
		}, function(){
		
			_self.t = setInterval(_self.timer, 4000);

		});
	}

}
