$(function(){
	hideFormText();
	initLightbox();	
	photoGallery();
	loadStoreDetail();
	
	$('#news-ticker marquee').marquee('pointer').mouseover(function () {
		$(this).trigger('stop');
	}).mouseout(function () {
		$(this).trigger('start');
	}).mousemove(function (event) {
		if ($(this).data('drag') == true) {
			this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
		}
	}).mousedown(function (event) {
		$(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
	}).mouseup(function () {
		$(this).data('drag', false);
	});
	
	if ($("#store-list a").length==0) {
		$("#store-list div.col").css({"width": "auto", "float": "left"});
	}
	
	var url = $.url();
	
	if (url.segment(1)=="directory" && url.segment(2)=="category") {
		$("#store-list a").each(function() {
			if ($(this).hasClass(url.segment(3))) {
				//$(this).addClass("active");
			} else {
				$(this).hide();
			}
		});
	}
	
	if (url.segment(1)=="directory" && !url.segment(3)) {
		$("#store-list a#"+url.segment(2)).click();
	}
	
	$('area[id]').each(function() {
		$(this).qtip({		
			position: {
				my: 'bottom center',
				at: 'top center',
				effect: false
			},
			content: {
				text: 'Loading...',
				ajax: {
					url: '/directory/tooltip-detail/'+$(this).attr("id")
				}
			},
			show: {
				solo: true,
				event: 'click mouseenter'
			},
			hide: {
				delay: 2000
			},
			style: {
				classes: 'ui-tooltip-westave',
				tip: {
					height: 8,
					width: 16
				}
			}
			
		});
	});
	
	$('#selstore select').bind('change', function () {
        var id = $(this).val();
		
        if (id != '') {
			if ($("area#"+id).closest("div.maps").is(":hidden")) {
				$("#levels span").toggleClass("active");
				$("div.maps").toggle();
			}			
			$("area#"+id).click();
        }
        return false;
    });

		
	$("#levels span").click(function(){	
		$("#levels span").removeClass("active");
		$("div.maps").hide();
		$("div."+$(this).attr("id")).fadeIn();
		$(this).addClass("active");
	});
	
	equalHeight($("#blog .column.right, #blog .column.left, #blog #content"));

});


/* photo gallery */

function photoGallery() {

	if ($("body").is("#homepage")) {
	
		// Initialize Galleria
		$("#main").galleria({
			width: 989,
			height: 482,
			minScaleRatio: 1,
			showInfo: false,
			showCounter: false,
			thumbFit: false,
			thumbCrop: false,
			autoplay: 4000,
			extend: function() {  
				this.$('thumbnails').click(this.proxy(function() {
				   this.play();
				}));
				/*var scope = this;
				this.bind(Galleria.THUMBNAIL, function(e) {
					$(e.thumbTarget).unbind('click').bind('mouseover', function() {
						scope.next();
					});
				});*/
			}
		});
		
			
		$("#gallery-nav p").toggle(function() {
			$("#gallery-nav p").animate({ top: 100 }, "slow");
			$("#gallery-nav").toggleClass("opened closed").animate({ height: 129 }, "slow", "linear", function(){ $(".galleria-carousel").css({"zIndex": 3}).animate({opacity: 1}, "slow"); });
		}, function() {
			$(".galleria-carousel").css({"zIndex": -1}).animate({opacity: 0}, "slow");
			$("#gallery-nav p").animate({ top: 1 }, "slow");
			$("#gallery-nav").animate({ height: 31 }, "slow", "linear", function(){ $("#gallery-nav").toggleClass("closed opened"); });
		});
	
	}
}

/* hideFormText */
function hideFormText() {
	var _inputs = document.getElementsByTagName('input');
	var _txt = document.getElementsByTagName('textarea');
	var _value = [];
	
	if (_inputs) {
		for(var i=0; i<_inputs.length; i++) {
			if (_inputs[i].type == 'text' || _inputs[i].type == 'password') {
				
				_inputs[i].index = i;
				_value[i] = _inputs[i].value;
				
				_inputs[i].onfocus = function(){
					if (this.value == _value[this.index])
						this.value = '';
				}
				_inputs[i].onblur = function(){
					if (this.value == '')
						this.value = _value[this.index];
				}
			}
		}
	}
	if (_txt) {
		for(var i=0; i<_txt.length; i++) {
			_txt[i].index = i;
			_value['txt'+i] = _txt[i].value;
			
			_txt[i].onfocus = function(){
				if (this.value == _value['txt'+this.index])
					this.value = '';
			}
			_txt[i].onblur = function(){
				if (this.value == '')
					this.value = _value['txt'+this.index];
			}
		}
	}
}

/* equal heights */

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function loadStoreDetail() {
	$("#store-list a").click(function(){
		$("#store-list a").removeClass("active");
		$("#main").block({message: '<h4 class="notice"><img src="/images/loader-32.gif" /> Loading store...</h4>'}); 
		if ($("#store-details").is(":visible")) {
			$("#store-details").hide();
		}
		$("#store-details-loader").load("/directory/store-detail/" + $(this).attr("id"), function(){
			$("#main").unblock();
			$("#store-details").toggle("slide", 500);
		});
		$(this).addClass("active");
	});
}

// initlightbox

function initLightbox(){
	$('.with-popup').simplebox();
}

(function($) {
	$.fn.simplebox = function(options) { 
		return new Simplebox(this, options); 
	};
	
	function Simplebox(context, options) { this.init(context, options); };
	
	Simplebox.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				duration: 300,
				linkClose: 'a.close, a.btn-close, input.close',
				divFader: 'fader',
				faderColor: 'black',
				opacity: 0.7,
				wrapper: '#wrapper',
				linkPopap: '.link-submit'
			}, options || {});
			this.btn = $(context);
			this.select = $(this.options.wrapper).find('select');
			this.initFader();
			this.btnEvent(this, this.btn);
		},
		btnEvent: function($this, el){
			el.click(function(){
				if ($(this).attr('href')) $this.toPrepare($(this).attr('href'));
				else $this.toPrepare($(this).attr('title'));
				return false;
			});
		},
		calcWinWidth: function(){
			this.winWidth = $('body').width();
			if ($(this.options.wrapper).width() > this.winWidth) this.winWidth = $(this.options.wrapper).width();
		},
		toPrepare: function(obj){
			this.popup = $(obj);
			this.btnClose = this.popup.find(this.options.linkClose);
			this.submitBtn = this.popup.find(this.options.linkPopap);
			
			if ($.browser.msie) this.select.css({visibility: 'hidden'});
			this.calcWinWidth();
			this.winHeight = $(window).height();
			this.winScroll = $(window).scrollTop();
			
			this.popupTop = this.winScroll + (this.winHeight/2) - this.popup.outerHeight(true)/2;
			if (this.popupTop < 0) this.popupTop = 0;
			this.faderHeight = $(this.options.wrapper).outerHeight();
			if ($(window).height() > this.faderHeight) this.faderHeight = $(window).height();
			
			this.popup.css({
				top: this.popupTop,
				left: this.winWidth/2 - this.popup.outerWidth(true)/2
			}).hide();
			this.fader.css({
				width: this.winWidth,
				height: this.faderHeight
			});
			this.initAnimate(this);
			this.initCloseEvent(this, this.btnClose, true);
			this.initCloseEvent(this, this.submitBtn, false);
			this.initCloseEvent(this, this.fader, true);
		},
		initCloseEvent: function($this, el, flag){
			el.click(function(){
				$this.popup.fadeOut($this.options.duration, function(){
					$this.popup.css({left: '-9999px'}).show();
					if ($.browser.msie) $this.select.css({visibility: 'visible'});
					$this.submitBtn.unbind('click');
					$this.fader.unbind('click');
					$this.btnClose.unbind('click');
					$(window).unbind('resize');
					if (flag) $this.fader.fadeOut($this.options.duration);
					else {
						if ($this.submitBtn.attr('href')) $this.toPrepare($this.submitBtn.attr('href'));
						else $this.toPrepare($this.submitBtn.attr('title'));
					}
				});
				return false;
			});
		},
		initAnimate:function ($this){
			$this.fader.fadeIn($this.options.duration, function(){
				$this.popup.fadeIn($this.options.duration);
			});
			$(window).resize(function(){
				$this.calcWinWidth();
				$this.popup.animate({
					left: $this.winWidth/2 - $this.popup.outerWidth(true)/2
				}, {queue:false, duration: $this.options.duration});
				$this.fader.css({width: $this.winWidth});
			});
		},
		initFader: function(){
			if ($(this.options.divFader).length > 0) this.fader = $(this.options.divFader);
			else{
				this.fader = $('<div class="'+this.options.divFader+'"></div>');
				$('body').append(this.fader);
				this.fader.css({
					position: 'absolute',
					zIndex: 999,
					left:0,
					top:0,
					background: this.options.faderColor,
					opacity: this.options.opacity
				}).hide();
			}
		}
	}
}(jQuery));
