$(function () {
	
	// Preload images
	$.preloadCssImages();
	
	
	// CSS tweaks
	$('#header #nav li:last').addClass('nobg');
	$('.block_head ul').each(function() { $('li:first', this).addClass('nobg'); });
	$('.block table tr:odd').css('background-color', '#fbfbfb');
	$('.block form input[type=file]').addClass('file');
			
	
	// Web stats
	$('table.stats').each(function() {
		var statsType;
		
		if($(this).attr('rel')) { statsType = $(this).attr('rel'); }
		else { statsType = 'area'; }
		
		$(this).hide().visualize({		
			type: statsType,	// 'bar', 'area', 'pie', 'line'
			width: '880px',
			height: '240px',
			colors: ['#6fb9e8', '#ec8526', '#9dc453', '#ddd74c']
		});
	});
	
	
	// Check / uncheck all checkboxes
	$('.check_all').click(function() {
		$(this).parents('form').find('input:checkbox').attr('checked', $(this).is(':checked'));   
	});
		
	
	// Set WYSIWYG editor
	$('.wysiwyg').wysiwyg({css: "/resources/css/wysiwyg.css"});
	
	
	// Modal boxes - to all links with rel="facebox"
	$('a[rel*=facebox]').facebox()
	
	
	// Messages
	$('.block .message').hide().append('<span class="close" title="Dismiss"></span>').fadeIn('slow');
	$('.block .message .close').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
		
	$('.block .message .close').click(function() {
		$(this).parent().fadeOut('slow', function() { $(this).remove(); });
	});
	
	
	// Form select styling
	$("form select.styled").select_skin();
	
	
	// Tabs
	$(".tab_content").hide();
	$("ul.tabs li:first-child").addClass("active").show();
	$(".block").find(".tab_content:first").show();

	$("ul.tabs li").click(function() {
		$(this).parent().find('li').removeClass("active");
		$(this).addClass("active");
		$(this).parents('.block').find(".tab_content").hide();

		var activeTab = $(this).find("a").attr("href");
		$(activeTab).show();
		return false;
	});
	
	
	// Sidebar Tabs
	$(".sidebar_content").hide();
	$("ul.sidemenu li:first-child").addClass("active").show();
	$(".block").find(".sidebar_content:first").show();

	$("ul.sidemenu li").click(function() {
		$(this).parent().find('li').removeClass("active");
		$(this).addClass("active");
		$(this).parents('.block').find(".sidebar_content").hide();

		var activeTab = $(this).find("a").attr("href");
		$(activeTab).show();
		return false;
	});	
	
	
	// Block search
	$('.block .block_head form .text').bind('click', function() { $(this).attr('value', ''); });
	
	
	// Image actions menu
	$('ul.imglist li').hover(
		function() { $(this).find('ul').css('display', 'none').fadeIn('fast').css('display', 'block'); },
		function() { $(this).find('ul').fadeOut(100); }
	);

		
	
	// Date picker
	$.extend(DateInput.DEFAULT_OPTS, {
	  stringToDate: function(string) {
	    var matches;
	    if ( matches = string.match(/^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/) ) {
	    	return new Date( matches[3], matches[1]-1, matches[2] );
	    } else {
	    	return null;
	    };
	  },
	  
	  dateToString: function(date) {
	    var month = (date.getMonth() + 1).toString();
	    var dom = date.getDate().toString();
	    if (month.length == 1) month = "0" + month;
	    if (dom.length == 1) dom = "0" + dom;
	    return month + "/" + dom + "/" + date.getFullYear();
	  }
	});
	$('input.date_picker').date_input();
	

	// Navigation dropdown fix for IE6
	if(jQuery.browser.version.substr(0,1) < 7) {
		$('#header #nav li').hover(
			function() { $(this).addClass('iehover'); },
			function() { $(this).removeClass('iehover'); }
		);
	}
	
	// IE6 PNG fix
	$(document).pngFix();
	
	// Convert HTML to Text
	$('#html_to_text').click(function(){
		$('#text_email').val($('#html_email').val().replace( /<\/?[^>]+>/gi, "\n").replace( /\n{2,}/gi, "\n\n" ) );
	});
	$('#html_preview').click(function(){

		var generator=window.open('','Email Preview','height=440,width=540,status=no,location=no,menubar=no,resizable=no,toolbar=no');
		generator.document.write('<html><head><title>Email Preview</title>');
		generator.document.write('<link rel="stylesheet" href="/resources/css/email.css">');
		generator.document.write('</head><body><div id="Title">Email Preview</div><div id="Container">');
		generator.document.write($('#html_email').val());
		generator.document.write('</div></body></html>');
		generator.document.close();
	});
		
});
