(function($) {
	this.screenshotPreview = function(){	
		$("a.screenshot").click(function(e){
			if($("#screenshot")) { $("#screenshot").remove(); }
			$("body").append("<p id='screenshot'><a href='javascript:void(0)' class='closeit'>X</a></p>");
			$("<img src='"+ this.rel +"' alt='' />").appendTo('#screenshot').load(function() {
				var wide = ($(window).width() / 2) - ($(this).width() / 2);
				var high = ($(window).height() / 2) - ($(this).height() / 2);			
				$("#screenshot").css({
					"top": high + "px",
					"left": wide + "px",
					"visibility": "visible"
				});
			});
		});
		$("a.closeit").live("click", function(){
			 $("#screenshot").remove();
		});
	};
	  
	$(document).ready(function() {
		$('#tabbed').tabs();
		$(".postbox .inside").not('#themeresources.postbox .inside, #themepresets.postbox .inside, #themefontsamples.postbox .inside').hide();
		$('.postbox h3, .postbox .handlediv').click(function() {
			$(this).parent().find('.inside').slideToggle('fast');
		});
		$('.postbox input, .postbox textarea, .postbox select').change(function() {
			$('#savetheme').addClass('remember');
		});
		var z = 100;
		$(".postbox").each(function() {
			$(this).css("zIndex", z);
			z--;
		});
		$('a.openall').click(function() {
			$(this).parent().parent().find('.postbox .inside').slideDown();
			$(this).hide();
			$(this).parent().find('a.closeall').show();
		});
		$('a.closeall').click(function() {
			$(this).parent().parent().find('.postbox .inside').slideUp();
			$(this).hide();
			$(this).parent().find('a.openall').show();	
		});	
		$("#savetheme").click(function() {
			$(".themesbybavotasan").trigger('submit');
			return false;
		});
		$("#savecss").click(function() {
			$(".custom_css_form").trigger('submit');
			return false;
		});
		$(".themesbybavotasan, .custom_css_form").submit(function(){
			$(".ajaxsave").show();
			var data = {
				action: 'save_theme_options',
				option: $(this).serialize()
			};
			$.post(ajaxurl, data,
			function(response){
				$('#savetheme').removeClass('remember');
				$("#message").show();
				$(".ajaxsave").hide();		
				setTimeout(function() { $('#message').slideUp(); }, 5000);
				headerLogo();
			});
			return false;
		});	
		function headerLogo() {
			if($('input[name="logo_header"]').val()) {
				var image = $('input[name="logo_header"]').val();
				$(".headerlogo").html('<img src="'+image+'" alt="" />');
			} else {
				$(".headerlogo img").remove();
			}
		}
		headerLogo();
		$('#leftcol_width').change(function() {
			if($(this).val()>100) {
				$(this).val(100)
			}
			var rightCol = 100 - $(this).val();
			$('#rightcol_width').val(rightCol);
		});
	
		$('input.picker').ColorPicker({
			onSubmit: function(hsb, hex, rgb, el) {
				if($(el).val()!=hex) {
					$('#savetheme').addClass('remember');
				}
				var picked = $(el).attr('name');
				$(el).val(hex.toUpperCase());
				$(el).ColorPickerHide();
				$("#"+picked).css('backgroundColor', "#"+hex);
			},
			onBeforeShow: function () {
				$(this).ColorPickerSetColor(this.value);
			}
		});
	
		$("input.picker").change(function() {
			var hex = $(this).val().toUpperCase();
			var picked = $(this).attr('name');
			$(this).val(hex);
			$("#"+picked).css('backgroundColor', "#"+hex);
		});
		
		$(".wrap").fadeIn(500);

		// ask the user to confirm the window closing
		window.onbeforeunload = function() {
			if($('#savetheme').attr('class')=="remember") {
				return "If you do, all of your changes will be lost.";
			}
		}
		function sidebarLoc() {
			if($('input[name="sidebar_width2"]').val() > 0) {
				$('#twoSidebar').show();
				$('#oneSidebar').hide();
				if($('input[name="sidebar_location1"]:checked').val() < 3) {
					$(".locerror").html('Please select a location for your sidebars.');											
				} else {
					$(".locerror").html();
				}
			} else {
				$('#twoSidebar').hide();
				$('#oneSidebar').show();
				if($('input[name="sidebar_location1"]:checked').val() > 2) {
					$(".locerror").html('Please select a location for your sidebar.');											
				} else {
					$(".locerror").html();
				}
			}
		}
		sidebarLoc();
		$('input[name="sidebar_width2"]').change(function() {
			sidebarLoc();
		});
	});
	screenshotPreview();
})(jQuery)