$(document).ready(function() {
	$('.modal').modal();
	$('.modal_custom').modal({width:700,showSpeed:2000,closeSpeed:2000,title:false,skin:"red"});
	$('a.ajax_link').click(function() {
		var url = $(this).attr("href");
		$.ajax({
			type: "POST",
			url: url,
			success: function(msg){
				$().successMessage({text:msg});
			},
			error: function(xhr, desc, exceptionobj){
				$().errorMessage({time:5000,text:"Something wrong in here:"+xhr.responseText});
			}
		});
		return false;
	});
	
	$('a.ajax_link_delete').click(function() {
		var url = $(this).attr("href");
		var id = $(this).attr("id");
		var split = id.split("_");
		var element_id = "#id_"+split[1];
		$.ajax({
			type: "POST",
			url: url,
			success: function(msg){
				$(element_id).fadeOut("0",function(){
					$().successMessage({time:2000,text:"Deleted!"});
				});
			},
			error: function(xhr, desc, exceptionobj){
				$().errorMessage({time:5000,text:"Something wrong in here:"+xhr.responseText});
			}
		});
		return false;
	});	
	
	$("a.form_submit").live("click", function(){
		var id = $(this).attr("id");
		var split = id.split("_");
		var form_id = "#"+split[0];
		$(form_id).submit();
		return false;
	});	
	
	// GMI
	
	// thumbs hover
	
	$("#loginPopup").click(function(){
		$('#loginWrapper').css('opacity', .8);
		$("#loginWrapper").show();	
		$('.loginForm').css('opacity', 1);
		
	});
	
	$(".thumbs li").mouseover(function(){
		$(".titleBox", this).hide();
	}).mouseout(function(){
		$(".titleBox", this).show();
	});
	
	$("div.getInfo").hide();
	$("#buyItem").click(function(){
		$("div.getInfo").toggle('slow');
		return false;
	});
	
	$(".changeSortType").change(function()
	{
		var category = $(".changeSortType").attr('name');
		var sort = $(".changeSortType").val();
		var type = $(".changeSortType").attr('id');
		window.location='http://welcomia.com/category/'+type+'/'+category+'/'+sort;
	});
	
	$(".changeTagSortType").change(function()
	{
		var id = $(".changeTagSortType").attr('name');
		var sort = $(".changeTagSortType").val();
		window.location='http://welcomia.com/tag/'+id+'/'+sort;
	});
	
	$(".changePhotosPerPage").change(function()
	{
		var category = $(".changePhotosPerPage").attr('name');
		var perPage = $(".changePhotosPerPage").val();
		var type = $(".changePhotosPerPage").attr('id');
		window.location='http://welcomia.com/category/'+type+'/'+category+'/0/'+perPage;
	});
	
		// lightbox

		function reloadSelect(){
			$.getJSON('/lightbox/list', function(data){
			
				$("#selectLightbox").removeOption(/./);
				
				for (var i = 0; i <= data.length; i++) {
					$("#selectLightbox").addOption(data[i].id, data[i].name);
				}
				
				if (data.length == 0) {
					$("#addImage2Lightbox").attr('disabled', true).css('cursor', 'regular');
				}
			});
		}		
		
		reloadSelect();
		$(".lightboxControl").hide();
		$(".buttonLightbox").click(function()
		{
			$(".lightboxControl").toggle();
		});
		$(".lightboxControl #createForm").hide();
		
		$(".lightboxControl #showCreate").click(function(){
			$(".lightboxControl #createForm").toggle(400);
			
		});
		
		$("#addImage2Lightbox").click(function()
		{
			var lightboxID = $("#selectLightbox").val();
			var fileID = $("#selectForm").attr("name");
			
			$.ajax({
				   type: "POST",
				   url: "/lightbox/addImage",
				   data: "fileID="+fileID+"&lightboxID="+lightboxID,
				   success: function(msg){
				     	$(".lightboxControl p.info").html('Success.');
						$(".lightboxControl p.info").show();
				   },
				   error: function(msg){
				     	$(".lightboxControl p.error").html('Error.');
						$(".lightboxControl p.error").show();
				}
			});
		});

		$("#createLightbox").click(function(){
			
			var newLightboxName = $(".lightboxControl #lightboxName").val();
			
			if(newLightboxName.length == 0)
			{
				$(".lightboxControl p.error").prepend('Error.');
				$(".lightboxControl p.error").show();
				
				setTimeout(function() { 
					$('.lightboxControl p.error').fadeOut(); 
				}, 2000);

			}
			else {
				$.ajax({
				   type: "POST",
				   url: "/lightbox/create",
				   data: "name="+newLightboxName,
				   success: function(msg){
				     	$(".lightboxControl p.info").html('Lightbox created.');
						$(".lightboxControl p.info").show();
				   },
				   error: function(msg){
				     	$(".lightboxControl p.error").html('Error.');
						$(".lightboxControl p.error").show();
				   }
				 });
				 
				 reloadSelect();
				 
				setTimeout(function() { $('.lightboxControl p.error').fadeOut(); }, 3000);
				setTimeout(function() { $('.lightboxControl p.info').fadeOut(); }, 3000);
			}
		});	
});


(function($){  
 $.fn.errorMessage = function(options) {  
  
  var defaults = {  
   time: 2000,
   text: "Something Wrong in here",
   target: "#messages"
  };  
  var options = $.extend(defaults, options);  
  var id = Math.ceil(Math.random()*10000);
  return this.each(function() {  
		var html = '<div class="error" id="'+id+'" style="display:none"><div class="head"><div></div></div><div class="desc"><p>'+options.text+'</p></div><div class="bottom"><div></div></div></div>';
		$(options.target).append(html);
		$("#"+id).slideDown(function(){
			setTimeout( function()
			{
				$("#"+id).slideUp(function(){
					$("#"+id).remove();
				});
			}, options.time);
		});  
  });  
 };  
 $.fn.successMessage = function(options) {  
  
  var defaults = {  
   time: 2000,
   text: "Success!",
   target: "#messages"
  };  
  var options = $.extend(defaults, options);  
  var id = Math.ceil(Math.random()*10000);
  return this.each(function() {  
		var html = '<div class="success" id="'+id+'" style="display:none"><div class="head"><div></div></div><div class="desc"><p>'+options.text+'</p></div><div class="bottom"><div></div></div></div>';
		$(options.target).append(html);
		$("#"+id).slideDown(function(){
			setTimeout( function()
			{
				$("#"+id).slideUp(function(){
					$("#"+id).remove();
				});
			}, options.time);
		});  	
  });  
 };  
 $.fn.noticeMessage = function(options) {  
  
  var defaults = {  
   time: 2000,
   text: "Notice!",
   target: "#messages"
  };  
  var options = $.extend(defaults, options);  
  var id = Math.ceil(Math.random()*10000);
  return this.each(function() {  
		var html = '<div class="notice" id="'+id+'" style="display:none"><div class="head"><div></div></div></div><div class="desc"><p>'+options.text+'</p></div><div class="bottom"><div></div></div></div>';
		$(options.target).append(html);
		$("#"+id).slideDown(function(){
			setTimeout( function()
			{
				$("#"+id).slideUp(function(){
					$("#"+id).remove();
				});
			}, options.time);
		});  	
  });  
 }; 
})(jQuery);

