function getChange($Page, $Type, $icons){
	var $MinSize  = $("#Size [name=minsize]").val();
	var $MaxSize  = $("#Size [name=maxsize]").val();
	var $Location = window.location.href;
	    $Location = $Location.replace(/#(.*)/, '');
	    $Type     = ($Type == undefined) ? $(".TypeOptions").val() : $Type;
	
	$.get($Location, {style : 2, type : $Type, Minsize : $MinSize, Maxsize : $MaxSize, page : $Page, icons : $icons}, function(Data){
		$("#Wrapper").fadeOut(function(){
			$(this).html(Data).fadeIn()
		});
		
	});
}

function CheckEmail(Email){
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(Email);
}
$(document).ready(function(){

	$(".Up_Menu a").click(function(){
		var $ul = $(this).parent().find("ul");
		$ul.slideDown("fast");
		
		if($ul.length > 0)
			return false;
	}).parent().hover(null, function(){
		$(this).find("ul").slideUp('slow');
	});
	
	$("#SearchText").focus();
	$(".Search").submit(function(){
		if($("#SearchText").val() == "" || $("#SearchText").val() == "..."){
			alert('Empty Search');
			return false;
		}
	});
	
	$(".ac_results").css({opacity: "0.8"});
	
	// Mark icons
	$(".Mark").live('click', function(){
		if($(this).is('.disable')) return false;
		$(this).addClass('disable');
		$.get($URL +'/Favorite/add', {icon: $(this).attr("title")});
		
		$(this).find(".New").show().animate({marginTop:"-20px"}, 300).hide("slow", function(){
			$(this).css("margin-top", 0);
		});
	});
	
	// Loding
	//## $(".Loading").ajaxStart(function(){ $(this).slideDown(); }).ajaxStop(function(){ $(this).fadeOut("fast"); });
	
	// Bg Color
	$(".BgColors li").live('click', function(){
		var $Bg;
		if($(this).attr("class") != "more_colors"){
			if($(this).attr("class") == "Transparent")
				$Bg = $(".Transparent").css("background-image"); 
			else
				$Bg = $(this).css("background-color");
			$(".icon").css("background", $Bg);
			$Bg = $Bg.replace('"', "'").replace('"', "'");
			$.cookie("iconsBg", $Bg, { expires: 360, path: '/'});
		}
	});
	// Type
	$(".TypeOptions").change(function(){ getChange(); });
	
	// Pages
	var $PageHash = location.hash.split('-');
	if($PageHash[0] == '#page' && $PageHash[1]) getChange($PageHash[1]);
	if(!$(".PagesList").is(".Static")){
		$(".PagesList li").live("click", function(){
			if(!$(this).is(".Maximum")){
				var $Page = $(this).text();
				getChange($Page);
				$.history({'page':$Page});
				
				location.hash = 'page-' + $Page;
			}
			return false;
		});
		
		$.history.callback = function(reinstate, cursor) {
			var $Page = 1;
			
			if (typeof(reinstate) == 'undefined')
				$Page = 1;
			else
				$Page = parseInt(reinstate.page) || 1;
			getChange($Page);
		};
	}
	
	// tweets
	var $tweets = $(".tweets");
	if($tweets.length > 0)
	{
		function get_tweets($tweets)
		{
			$('.load', $tweets).show();
			$.get($URL +'/tweets/'+ $tweets.attr('title'), function(Tweets){
				$('ul', $tweets).slideUp(function(){
					$('.load', $tweets).hide();
					$(this).html(Tweets).slideDown();
					$('a', this).attr('target', '_blank');
				});
			});
		}
		get_tweets($tweets);
		
		$('.refresh', $tweets).click(function(){ get_tweets($tweets); });
	}

	$('.liberation').live('click', function(){
		$('div', this).fadeIn("fast").hover(null, function(){
			$(this).fadeOut('slow');
		});
	});
	
	$more_colors = $('.more_colors');
	if($more_colors.length > 0)
	{
		$more_colors.append('<ul></ul>');
		var $colors = ['f7d6d4','e7847f','df5b54','d73229','851e19','5a1511','fce1d0','f6a673','f38844','f16a15','95410c','652c08',
					   'fbfbd6','f4f483','f0f05a','eded31','92921d','636314','d5f6d8','80e389','56da62','2cd13b','1a8124','125818',
					   'ddf3f3','98dcdc','76d0d0','54c4c4','337979','235252','d6dfed','859ecb','5d7eba','345da9','1f3968','152747'];
		$.each($colors, function(index, color) { 
			$('ul', $more_colors).append('<li style="background:#'+color+';"/>');
		});

	}
});
