/**
 * Javascript map dynamic xml content
 *
 * author: Andrei Dinca
 * email: andrei@theadgency.ro
 *
 * version 1.0 release date: 
 *
**/

var gallery = {
	o: {
		elm 	: '.gallery-list',
		navHover : 'div.gallery-nav-wrapper',
		moveItems: 1,
		showItems: 4,
		itemsWidth: 160,
		currStep: 0,
		duration: {
			navHover: 400,
			carusel: 500
		},
		lightBoxHeight: 220,
		running: false,
		caruselRun: false,
		ajaxUrl: ''
	},
	// just init
	option:{},
	
	init: function(customOption) {
		var self = this;
		
		// extend default option
		self.option = $.extend({}, self.o, customOption);
		
		self.openLightboxObserver();
		
		// start carusel click observer - temp
		self.observCarusel();
		
	},
	
	openLightboxObserver: function(){
		var self = this;
		
		$(self.option.elm).find('a').click(function(){
			// open lightbox
			self.openLightBox($(this));
			
			// observe if is close
			$('.closeLightbox').live('click', function() {
				self.closeLightbox();
			});
		});
	},
	
	closeLightbox: function(){
		var self = this;
		
		$("div.lightbox-frame").fadeOut(400);
		
		// clean response box
		$('#ajaxResponse').html('');
		
		// close navigation
		$("div.gallery-nav-wrapper").css('display', 'none');
	},
	
	openLightBox: function($this){
		var self = this;

		// open loading
		$(".ajaxLoader").fadeIn(100);
						
		// remove last on class
		$(".gallery-list").find('.on').removeClass('on');
		
		// add class on for features
		$this.addClass('on');
		
		var running = false;

		var new_link = $this.attr('rel');
		$.ajax({
			type: "GET",
			url: self.option.ajaxUrl + '/request.php?img=' + new_link +'&width=' + ($(document).width()) + '&height=' + ($(document).height() - self.option.lightBoxHeight),
			dataType: "html",
			success: function(data) {
				
				// set html title
				$("#postName").find('span').html("<span style='font-size: 11px;'>" + ($this.parent('li').index() + 1) + "/" + $(".gallery-list li").size() + "</span> " + $this.attr('title'));
			
				// clean response box
				$('#ajaxResponse').html('');
				
				// load content
				$('#ajaxResponse').html(data);
				
				$("div.lightbox-frame").css('display', 'block');
				$('#maxImg').css('opacity', 0);
				// hack get image real width
				$('#maxImg').load(function () {
					if($(this).height() > 100) {
						//$('#maxImg').css('opacity', 1);
						
						
						var tmpW = $(this).width(); var tmpH = $(this).height(); 
						$("div.imageBlock").css('margin-left', "-" + (parseInt($("#maxImg").width()) / 2) + "px");
						$("div.imageBlock").css('margin-top', "-" + (parseInt($("#maxImg").height()) / 2) + "px");
						
						$("#maxImg").animate({
							opacity: 1
						}, 700, function(){
							// show navigation
							$("div.gallery-nav-wrapper").fadeIn(300);
						});
						
						/*
						$("div.maskTop").css('height', Math.floor(tmpH / 2));
						$("div.maskBottom").css('height', Math.floor(tmpH / 2));
						
						$("div.maskTop").animate({
							height: 0
						}, 700, 'easeOutCirc');
						
						$("div.maskBottom").animate({
							height: 0
						}, 700, 'easeOutCirc'
						*/
						
						// close loading
						$(".ajaxLoader").fadeOut(100);
						
						// setup next and back
						var brw = {
							width : $(document).width(),
							height : $(document).height()
						}
						$(".fullGallery-prev, .fullGallery-next").css({
							width: (brw.width / 2) + "px",
							height: (brw.height - 20) + "px"
						});
						
						
						$(".fullGallery-prev").each(function(){
							// get current open element
							var openElmIndex = $(".gallery-list").find('.on').parent('li').index();
							
							var nextElm = openElmIndex - 1;
							
							// change href attr
							$(this).attr('href', $(".gallery-list > li a").eq(nextElm).attr('href'));
						});
						
						$(".fullGallery-next").each(function(){
							// get current open element
							var openElmIndex = $(".gallery-list").find('.on').parent('li').index();
							
							var nextElm = openElmIndex + 1;
							var __lenght = $(".gallery-list").find('li').size();
							if(nextElm >= __lenght){
								nextElm = 0;
							}
							
							// change href attr
							$(this).attr('href', $(".gallery-list > li a").eq(nextElm).attr('href'));
						});
						
						
						running = false;
					}					
				});
			}
		});
		
		return true;
	},
	
	observCarusel: function(){
		var self = this;
		
		$(".gallery-categ a").hover(function(){
			$(this).find('span').css('opacity', 1);
			$(this).find('img').animate({
				opacity: 0.5
			}, 150);
		}, function(){
			$(this).find('img').animate({
				opacity: 1
			}, 50);
		})
		
		$(".left-menu li").click(function() {
			$(this).find('ul').slideDown(200);
		});
	}	
}

this.tooltip = function(){
	xOffset = 25;
	yOffset = -14;
	$("a.tooltip").hover(function(e){
			this.t = this.title;
			this.title = "";
			$("body").append("<p id='tooltip'>"+ this.t +"</p>");
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.css('display', 'block');
		
    },
	function(){
		this.title = this.t;
		$("#tooltip").remove();
    });
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
			
		if(e.pageY < 55){
			$("#tooltip").css('font-size', '0px');
		}else{
			$("#tooltip").css('font-size', '11px');
		}	
	});	
};

