$(document).ready(function() {	
	
	//------- start Вывод блока авторизации -------//
	$('#loginPopup').hide();
	$('#authclick').click(function(event){
	    event.preventDefault();
	    event.stopPropagation();
	    $('#loginPopup').show();
	})
	
	$('#loginPopup').click(function(event){
		event.stopPropagation();
	})
	
	$(document).click(function(){
	    $('#loginPopup').hide();
	})
	//end Вывод блока авторизации //
	
	//------- start Вывод информации об обоине в списке обоев -------//
	$("span.iLink").click(function(){
//		$(this).parents(".text").find("ul.info").slideToggle();
		$(this).parents(".text").find("ul.info").fadeToggle();
	})
	//end Вывод информации об обоине в списке обоев //
	
	//------- start Блок "Форматы обоев" справа -------//
	$(".FormatWallpaperBox li.links a").click(function(){
//		var title=$(this).attr('title');
		var title=$(this).attr('id');
		$(".FormatWallpaperBox li.links a").removeClass("active");
		$(this).addClass("active");
		
		$(".FormatWallpaperBox li.screens").removeClass("active");
		$(".FormatWallpaperBox li."+title).addClass("active");
	})
	//end Блок "Форматы обоев" справа //
	
	
	//------- start Вывод окошка "выбрать размер" на странице обоины  -------//
	$("div.wallpaperInfo div#selectSize div.head a.open,div#selectSize").click(function(event){
		event.stopPropagation();
	});
	
	$("div.wallpaperInfo div#selectSize div.head a.open").click(function(){
		$('div#selectSize').animate({
			width: "320",
			height: "260"
			}, 500);
	});
	
	$(document).click(function(){
	    $('div.wallpaperInfo div#selectSize').animate({
			width: "122",
			height: "24"
			}, 500);
	})
	//end Вывод окошка "выбрать размер" на странице обоины //

						//------- start Вывод окошка "выбрать размер" на странице обоины  -------//
						$("td.theInfo div#selectSize div.head a.open,div#selectSize").click(function(event){
							event.stopPropagation();
						});
						
						$("td.theInfo div#selectSize div.head a.open").click(function(){
							$('div#selectSize').animate({
								width: "320",
								height: "200",
								left: "-63"
								}, 500);
						});
						
						$(document).click(function(){
						    $('td.theInfo div#selectSize').animate({
								width: "158",
								height: "24",
								left: "0"
								}, 500);
						})
						//end Вывод окошка "выбрать размер" на странице обоины //
	
	
		var wallWW = $("div#wallpaperPicWrap").width();
		

		if ((wallWW-690) > 210) {
			$("div#adv").show();
		} 

		auto_select_format();
})

$(window).resize(function() {
	var wallWW = $("div#wallpaperPicWrap").width(); 
	
	if ((wallWW-700) > 210) {
		$("div#adv").show();
	} else {
		$("div#adv").hide();
	}
})

function auto_select_format(){
	if(typeof(screen)=='undefined') return;
	var myfmt = screen.width + 'x' + screen.height;

	var found = 0;
	$('.FormatWallpaperBox li.screens').each(function(k){
		var li_formats = $(this);
		$('a', li_formats).each(function(j){
			if(found == 1) return;
			var a_text = $(this).text();

			if(myfmt == a_text){
				//Highlight My Tab
				$(".FormatWallpaperBox li.links a").removeClass("active");
				var li_cl = li_formats.attr('class');
				li_cl = li_cl.replace(/(screens|active|\s)/g, '');
//				$(".FormatWallpaperBox li.links a[title='"+li_cl+"']")
				$("#"+li_cl)
					.addClass("active")
					.css({"font-weight": "bold" });

				//Select (show) My Tab
				$(".FormatWallpaperBox li.screens").removeClass("active");
				li_formats.addClass("active");

				//Highlight my format
				$(this).css({"font-weight": "bold" });

				found = 1;
			}

			
		});

	});
}

function login_user(obj){
	var data = {'ajax' : 1, 'rnd' : Math.random() };
	$('#loginPopup :input').each( function (i){
		var o = $(this);
		data[String(o.attr('name'))] = String(o.val());
	});
	$.ajaxSetup({ 
	  type: "POST"
	});
	
	$('#login_info').load('/login.html', data);
	return false;
}

function send_to_friend(){
	var data = {'ajax' : 1, 'rnd' : Math.random() };
	$('#send_to_friend :input').each( function (i){
		var o = $(this);
		data[String(o.attr('name'))] = String(o.val());
	});
	$.ajaxSetup({ 
	  type: "POST"
	});

	jQuery.getJSON('/ajax.php', data, function(rdata){
		try{
			if(rdata.result == "1"){//OK
			}else{//ERROR
			}
//			$('#send_to_friend_msg').html(rdata.msg);
			alert(rdata.msg);
		}catch(e){
		}
	});

	return false;
}

