// clear form function

$.fn.clearForm = function() {

	return this.each(function() {

		var type = this.type, tag = this.tagName.toLowerCase();

		var id = this.id

		if (tag == 'form')

			return $(':input',this).clearForm();

		if (type == 'text' || type == 'password' || tag == 'textarea' || id == 'id')

			this.value = '';

		else if (type == 'checkbox' || type == 'radio')

			this.checked = false;

		else if (tag == 'select')

			this.selectedIndex = 0;

	});

};



// RTE image uploader plugin

function fileBrowserCallBack(field_name, url, type, win) {

	browserField = field_name;

	browserWin = win;

	window.open('/images/', 'browserWindow', 'modal,width=600,height=400,scrollbars=yes');

}

		

// initiate tinyRTE

function RTEsetup(editorClass,type) {

	// Theme options

		if(type == "simple"){

			var buttons="bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,link,unlink,|,paste,pastetext,pasteword,search,|,code";

		}else{

			var buttons="bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,anchor,link,unlink,|,image,table,|,paste,pastetext,pasteword,search,|,code,|,formatselect";

		}

		

	tinyMCE.init({

		// General options

		mode : "textareas",

		theme : "advanced",

		editor_selector : editorClass,

		plugins : "table,advlink,inlinepopups,paste,searchreplace,contextmenu,ibrowser",



		// Theme options

		theme_advanced_buttons1 : buttons,



		theme_advanced_buttons2 : '',

		theme_advanced_buttons3 : '',

		theme_advanced_buttons4 : '',

		theme_advanced_toolbar_location : "top",

		theme_advanced_toolbar_align : "left",

		theme_advanced_statusbar_location : "bottom",

		theme_advanced_resizing : true,

		extended_valid_elements : 'a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]',

		file_browser_callback: 'fileBrowserCallBack', 



		// Example content CSS (should be your site CSS)

		content_css : "/css/rte.css"

	});

}



function createPop(){

	$('.create-form').css({'left': ($(window).width()/2)-253}).fadeIn('fast');

	$('.create-form').fadeIn('fast');

	$('.create-form .buttons button').eq(0).children('span').text('Create');

}



$(function(){

	// check if search has more than 3 characters

	$('.search-btn').click(function(){

		var str = $('.search-form input').val();

		if(str == "search" || str.length < 3){

			alert('Please enter at least 3 characters in the search field');

			return false;

		}

	});

	

	$('.delete').live('click', function(){

		if(!confirm("Are you sure you want to delete this record?")){

			return false;

		}

	});



	// add clicks

	$('.adclickbanner').click(function(){

		var adId = $(this).attr('rel');

		$('#ajax').load('/article/x/ad/clickbanner/' + adId, null, function() {

			//Marvel.message.flash($('#xFlash').html());

		});

	});

	$('.adclicktext').click(function(){

		var adId = $(this).attr('rel');

		$('#ajax').load('/article/x/ad/clicktext/' + adId, null, function() {

			//Marvel.message.flash($('#xFlash').html());

		});

	});

	

	// for IE add rounded corners to all rounded elements

	if($('#ie-wrapper').length > 0){

		$('.btn-link, .buttons button').each(function(){

			$(this).prepend('<div class="tlc"></div><div class="trc"></div>').append('<div class="blc"></div><div class="brc"></div>').css('position', 'relative');

		});

	}

	

	// select search state according to current page

	if($('.blue').length > 0){

		$('.search-form select option').eq(1).attr('selected', 'selected');

	}else if($('.purple').length > 0){

		$('.search-form select option').eq(2).attr('selected', 'selected');

	}

	

	// initiate tooltips

	$('.tooltip').livequery( function(){

		$(this).tooltip({track: true,delay: 0,showURL: false, showBody: " ~ "});

	});

	

	$('#main-nav input').example(function() {

		if($(this).val() == ''){

			return $(this).attr('title');

		}

	}, {className: 'default-value'});

	

	var loginFail = self.document.location.hash.substring(1);

	if(loginFail == "login"){

		$('.login-form').slideToggle('fast');

		$('#header-nav').toggleClass('login-open');

	}

	if(loginFail == "errors"){

		createPop();

	}

	

	// open login panel

	$('a.login').click(function(){

		$('.login-form').slideToggle('fast');

		$('#header-nav').toggleClass('login-open');

		return false;

	});

	

	//enable form row highlighting

	$('.text input, .password input, .select select, .textarea textarea, .input input, .select select, .radio input, .checkbox input')

	.livequery('focus', function(){

			$(this).parents('.input, .form-element').addClass("over");

			$(this).parents('fieldset').addClass("active");

		}).livequery('blur', function(){

			$(this).parents('.input, .form-element').removeClass("over");

			$(this).parents('fieldset').removeClass("active");

		});

	

	// give each last li element in the page tree ul a class name

	$('#page-tree li:last-child').addClass('last'); 

	

	// show create form

	$('.create').live('click', function(){

		$('.create-form').css({'left': ($(window).width()/2)-253});

		$('.create-form form').clearForm();

		$('.create-form').fadeIn('fast');

		$('.insertdiv').show();

		$('.create-form .buttons button').eq(0).children('span').text('Create');

		return false;

	});

	

	// hide create form

	$('.create-form .cancel').live('click', function(){

		$('.create-form form').clearForm();

		$('.create-form').fadeOut('fast');

		return false;

	});

	

	$('#pop_overlay').live('click', function(){

		//$('.create-form, #pop_overlay, #login-popup').fadeOut('fast');

	});

});



// Marvel static functions

var Marvel = {

	// Html helper

	htmlHelper:{

		_vars:{

			loadingImage:'loading.gif',

			loadingImageBig:'loading.gif',

			loadingImageSmall:'loadingSmall.gif'

		},



		fadeReplace:function (source, target) {

			$(target).fadeOut(400);

			setTimeout(function () {

				$(target).html($(source).html());

				$(target).fadeIn(400);

			}, 450);

		},



		slideHide:function (target) {

			$(target).slideUp(800);

			setTimeout(function() { $(target).empty(); }, 900);

		},



		showLoading:function (target) {

			Marvel.htmlHelper.showCustomLoading(target, 'center', Marvel.htmlHelper._vars.loadingImageBig);

		},



		showCustomLoading:function (target, align, image) {

			if (!$(target).html().match(/loading/i)){

				$(target).html('<div id="loadingdiv" style="text-align:' + align + '"><img src="/img/' + image + '" alt="" /></div>');

			}

		}



	},



	// Page messaging via flash functionality

	message:{

		settings:{

			slideTime:900,

			displayTime:3000

		},



		flash:function (message) {

			// Make sure message is not empty

			message = $.trim(message);

			if (!message){

				return;

			}



			// Attach message

			if ($('#flash').html()) {

				$('#flash').append("<br>" + message);

			} else {

				$('#flashPoint').html('<div class="flash" id="flash">' + message + '</div>');

			}



			// Show

			if (!Marvel.message.isFlashing) {

				$('#flashPoint').hide();

				$('#flashPoint').slideDown(Marvel.message.settings.slideTime);

				Marvel.message.isFlashing = setTimeout(Marvel.message.hide, Marvel.message.settings.displayTime);

			} else {

				clearTimeout(Marvel.message.isFlashing);

				Marvel.message.isFlashing = setTimeout(Marvel.message.hide, Marvel.message.settings.displayTime);

			}

		},



		hide:function () {

			$('#flashPoint').fadeOut(Marvel.message.settings.slideTime);

			setTimeout(Marvel.message.clear, Marvel.message.settings.slideTime + 5);

			Marvel.message.isFlashing = false;

		},



		clear:function() {

			$('#flashPoint').empty();

		}

	},



	// Php clones (http://kevin.vanzonneveld.net/code/php_equivalents/php.js)

	php:{

		empty:function(mixed_var) {

			var key;

			

			if (mixed_var === ""

				|| mixed_var === 0

				|| mixed_var === "0"

				|| mixed_var === null

				|| mixed_var === false

				|| mixed_var === undefined

			){

				return true;

			}

			if (typeof mixed_var == 'object') {

				for (key in mixed_var) {

					if (typeof mixed_var[key] !== 'function' ) {

						return false;

					}

				}

				return true;

			}

			return false;

		}

	}

};
