$(function(){
	
	$("form[fancy='true'] select").fancy_select();
	$(".img").fancyImg();
	
});

$.fn.fancy_select = function(){
	var obj = $(this);
	if( obj.data("fancy") == "1" ){ return; }
	obj.data("fancy", "1");
	
	action( obj );
	
	function action( obj ){
		
		var label = obj.parent();
		var addons = obj.attr("class");

		var value = obj.find("[value='"+obj.attr('value')+"']:first").html() || obj.find("option:first").html();
		if (value) {
			value = value.split("[").join("<").split("]").join(">");
		}
		
		var output =	'<div class="select '+addons+'">'+
								'<div class="active"><div>'+ value +'</div></div>'+
								'<div class="more"><div class="scrollable">';
		
		
		
		obj.find("option").each(function(){

			var option = $(this);
			
			output+= '<a linked="'+option.attr("value")+'" class="option">'+option.text().split("[").join("<").split("]").join(">")+'</a>';
			
		});
		
		output+=	'</div></div>'+
					'</div>';
		
		obj.hide().parent().append( output );
		
		var new_select = obj.parent().find(".select");
		var shop_list = $(".shop_list");
		
		var flag = 0;
		new_select.click(function(e){
			
			if( $(this).is(".open") ){
				new_select.removeClass("open");
			}
			
			else{
				
				$(".select").removeClass("open");
				new_select.addClass("open");
								
				$(document).unbind("click");
					
				$(document).bind("click", function(){
					$(document).click(function(e){
						
						if( flag == 1 ){
							
							flag = 0;
						
						}
						else{
							$(".select").removeClass("open");
							$(document).unbind();
						}
						
					});
				});
					
				
		
			}
			
		}).end().find(".more a").click(function(e){
			
			
			obj.attr("value", $(this).attr("linked") );
			new_select.find(".clicked").removeClass("clicked");
			$(this).addClass("clicked");
			new_select.find(".active div").html( $(this).html() );
			
			if( obj.attr("autopost") == "true") {
				new_select.parents("form").submit();
			}
			
			
		});
		
		obj.find("option").click(function(){
			if( obj.attr("autopost") == "true") {
				new_select.parents("form").submit();
			}
		});		
		
	};

	
};

$.fn.fancyImg = function(){
	
	$(this).each(function(){
		
		var cont = $(this);
		var main = cont.find("img").not(".big");
		var imgs = cont.find(".big");
		var tags = cont.find("img[class!='big'], span");
		
		tags.bind("click", function(e){
			
			e.preventDefault();
			
			imgs.eq(0).trigger("click");
					
		});
		
		imgs.each(function(i, val){
			var src = $(this).attr("src");
			$(this).attr("count", i+1).removeAttr("src").attr("src2", src);	
			
		}).bind("click", function(){
			var flag = 0;
			var img = $(this);
			var tmp = new Image();
			
			$().notification({
				
				text: "<img src='/static/imgs/loader.gif' />",
				noclose : true
				
			});
			
			$(".opaque").bind("click", function(){
				
				flag = 1;
				
			});
			
			tmp.onload = function(){
				if( flag == 0 ){
					img_loaded( img, tmp.width, tmp.height);
				};
				
			};
			tmp.src = img.attr("src2");
			
			$(document).bind("keypress", function(e){
				
				var code = e.keyCode ? e.keyCode : e.which;
				
				if( code == 37 ){
					e.preventDefault();
					$("#notification .navigation .prev").trigger("click");
				}
				else if( code == 39 ){
					e.preventDefault();
					$("#notification .navigation .next").trigger("click");
				}
				else if( code == 27 ){
					e.preventDefault();
					$(".opaque").trigger("click");
				};
				
			});
			
		});
		
		
		function img_loaded( img, width, height){
			
			var text = "";
			imgs.removeClass("active");
			img.addClass("active");
			if( img.attr("title") ){
				text+= "<h3>"+img.attr("title")+"</h3>";
			};
			if( img.attr("author") ){
				text+= "<span class='author'>"+img.attr("author")+"</span>";
			};
			
			if( height+95 > $(window).height() ){ height = $(window).height() - 95; }
			
			text+= "<img src='"+img.attr("src2")+"' height='"+height+"' />";
			text+= "<div class='navigation'>";
			text+= "<a class='prev'>&laquo; Prev</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			
			text+= img.attr("count")+"/"+imgs.size();
			text+= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='next'>Next &raquo;</a>";
			text+= "</div>";
			
			main.find(".big:first").notification({
				
				text: text	
				
			});
			
			$("#notification .navigation").find(".prev, .next").disableTextSelect();
			navigation();
	
			
		};
		function navigation(){
		
			var notification = $("#notification");
			var navigation = notification.find(".navigation");
			var prev = navigation.find(".prev");
			var next = navigation.find(".next");
		
			prev.bind("click", function(){
				
				var count = 0;
				imgs.each(function(i, val){
					
					if( $(this).is(".active") ){
						
						count = i-1;
						return false;
					};
					
				});
				
				var obj = imgs.eq(count);
				
				if( !obj.attr("src2") ){ obj = imgs.last(); }
				obj.trigger("click");
				return false;
			});
			
			next.bind("click", function(){
				
				var count = 0;
				imgs.each(function(i, val){
					
					if( $(this).is(".active") ){
						
						count = i+1;
						return false;
					};
					
				});
				
				var obj = imgs.eq(count);
				
				if( !obj.attr("src2") ){ obj = imgs.first(); }
				obj.trigger("click");
				return false;
			});
		
		};
		
	});
	
};

$.fn.notification = function (options){
	
	//.notification{position:absolute;}
	//body > .notification { position: fixed; }
	$("#notification").attr("id", "old").fadeOut(function(){
		$("#old").remove();
	});
	
	if(options == null){options ={};}
	var o ={};
	o.text = $(this).html() || options['text'] || "Test text";
	
	o.fade = options['fade'] || 'false';
	o.rainbow = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
	o.t_layer = options['t_layer'] || 'true';
	o.t_lvl = options['t_lvl'] || '0';
	o.t_color = options['t_color'] || '#000';
	o.delay = options['delay'] || 3000;
	o.speed = options['speed'] || 500;
	o.clickTAG = options['clickTag'] || '.notification .close, .opaque';
	o.type = options['type'] || 'default';
	o.id = 'notification';
	o.flag = 'true';
	o.scroller = "true";
	if(options.t_color == "rainbow"){o.t_color = o.rainbow;}
	
	o.html = '<div class="notification" id="'+o.id+'">';
	
	if( options.noclose !== true ){
		
		o.html+= '<a class="close">&nbsp;</a>';
	}
	
	o.html+= o.text;
	o.html+= '</div>';

	$("body").append(o.html); 
	
	var notify = $("#notification");
	
	notify.css("visibility", "hidden");
	 
	if($(".notification").outerHeight() > $(window).height()-50){$(".notification").css({position:"absolute"}); o.scroller="false";}
	if($(".notification").css("position") == "absolute" && o.scroller == "true"){$(window).scroll(positions);}
	
	positions(false);

	$(window).resize(function(){
		
		positions( false );	
		
	});
	
	function positions( switcher ){
		if(o.flag == "true"){
			var pos = {};
			pos.left = ($(window).width() - $("#"+o.id).outerWidth())/2;
			pos.top = $(window).scrollTop() + (($(window).height() - $("#"+o.id).outerHeight())/2);
			if(pos.left < 1){ pos.left = 0;}
			if(pos.top <0 || $(".notification").outerHeight() > $(window).height()){ pos.top = $(window).scrollTop(); }
			if($(".notification").css("position") == "fixed"){ pos.top = (($(window).height() - $("#"+o.id).outerHeight())/2); }
			
			if(switcher == false){ $("#"+o.id).css({'left':pos.left+'px', 'top':pos.top+'px'}); }
			else{ $("#"+o.id).animate({'left':pos.left+'px', 'top':pos.top+'px'},{duration:300, queue:false});}
		}
		$(".opaque").css({
		
			"width" : $(window).width(),
			"height" : $(document).height()
		
		});
		
	}

	if(o.t_layer == "true"){
		if( !$(".opaque").html() ){
      	$("body").append('<div class="opaque" id="opaque_'+o.id+'">&nbsp;</div>');
		};
      $("#opaque_"+o.id).css({'position':'absolute', 'top':'0px', 'z-index':'900', 'left':'0px','background':o.t_color, 'height':$(document).height()+'px', 'width':$(document).width()+'px'}).fadeTo(0, o.t_lvl);
	}
	$(window).resize(positions);

	
	if(o.fade == "true"){fade_out(o.delay)}
   else{
      $(o.clickTAG).click(function(){fade_out(0);});
      $(o.clickTAG).css({'cursor':'pointer'});
   }
   function fade_out( delay ){
      $("#"+o.id).fadeTo(delay, "1.0").fadeOut(o.speed, function(){$(this).remove();});
      if(o.t_layer == "true"){
         $("#opaque_"+o.id).fadeTo(delay, o.t_lvl).fadeOut(o.speed, function(){$(this).remove();});
      }
		o.flag = "false";
		$(document).unbind("keypress");
   }
	
	notify.hide().css("visibility", "visible").fadeIn();
}

$.extend($.fn.disableTextSelect = function() {
	return this.each(function(){
		if($.browser.mozilla){//Firefox
			$(this).css('MozUserSelect','none');
		}else if($.browser.msie){//IE
			$(this).bind('selectstart',function(){return false;});
		}else{//Opera, etc.
			$(this).mousedown(function(){return false;});
		}
	});
});




