var ajax_fn = '';

$(document).ready(function() 
{
	// Drag table rows and build serialized_order
	if ($("#drag_table").length) {
	    $("#drag_table").tableDnD( {
	    	onDragClass: "drag_class",
	    	onDrop: function(table, row) {
				$("#serialized_order").val($.tableDnD.serialize());
	    	}
	    });
	}
	
	// Add zebra style to tables
	$(".standardTable tr").live("mouseover",
		function() {
			$(this).addClass("row_highlight");
		}
	)
	$(".standardTable tr").live("mouseout",
		function() {
			$(this).removeClass("row_highlight");
		}
	)

	// Check/uncheck row of a table
	$(".standardTable tr td").live("click", 
		function(event) {
			if ( this == event.target) {
				if ($(this).parent().find(':first-child:first-child input[type=checkbox]').length) {
					chkbx = $(this).parent().find(':first-child:first-child input[type=checkbox]');
					if ($(chkbx).attr("checked")) {
						$(chkbx).attr("checked", "");
					} else {
						$(chkbx).attr("checked", "checked");
					}
				}
			}
		}
	);
	// Check/uncheck all checkboxes
	if ($(".standardTable tr th input[type=checkbox]").length) {
		$(".standardTable tr th input[type=checkbox]").click( function() {
			chkbxs = $(this).parent().parent().parent().find("tr td:first-child:first-child input[type=checkbox]");
			if (chkbxs.length) {
				if ($(this).attr("checked")) {
					chkbxs.each( function() {
						$(this).attr("checked", "checked");
					});
				} else {
					chkbxs.each( function() {
						$(this).attr("checked", "");
					});
				}
			}
		});
	}
	
	// Highlight errors
	if (isset('js_highlights')) {
		$.each(js_highlights , function(index, value) {
			$("input[name="+this+"]").addClass('error_highlight');
			$("textarea[name="+this+"]").addClass('error_highlight');
			$("select[name="+this+"]").addClass('error_highlight');
		});
	}

	// 'Write in Seo Style' button
	$(".seo_rewrite").click( function() {
		if ($().jquery < '1.3') {
			name = $("input[@name=name]").val().toLowerCase();
		} else {
			name = $("input[name=name]").val().toLowerCase();
		}
		seo_name_write(name);
	});

	// Auto resize wrong listings' logos
	$(".img_div, #img_div").each( function() {
		div_width = $(this).width();
		div_height = $(this).height();
		
		img = $(this).find("a img");
		
		img_width = img.width();
		img_height = img.height();
		img.css("width", "auto").css("height", "auto"); // Remove existing CSS
        img.removeAttr("width").removeAttr("height");
		
		if (div_width < img_width) {
			img.css('width', div_width);
		} else {
			if (div_height < img_height) {
				img.css('height', div_height);
			}
		}
	});
	
	// Move slowly through anchors
	$("a.ancLinks").click(function () {
		elementClick = $(this).attr("href");
		destination = $(elementClick).offset().top;
		$("html").animate({ scrollTop: destination}, 1100 );
		return false;
    });
    
    if (window.$.datepicker != undefined) {
	    $.datepicker._generateHTML_Old = $.datepicker._generateHTML; $.datepicker._generateHTML = function(inst) {
			res = this._generateHTML_Old(inst); res = res.replace("_hideDatepicker()","_clearDate('#"+inst.id+"')"); return res;
		}
    }
    
    $(".popup_dialog").live('click', function() {
    	ajax_loader_show();
    	url = this.href;
    	title = this.title;
    	var dialog = $('<div style="display:hidden"></div>').appendTo('body');
    	// load remote content
    	dialog.load(
    		url,
    		{},
    		function (responseText, textStatus, XMLHttpRequest) {
    			dialog.dialog({title: title, draggable: false, modal: true, width: 580});
    			ajax_loader_hide();
    		}
    	);
    	//prevent the browser to follow the link
    	return false;
	});
	
	// Modal window
	if ($("a.nyroModal").length) {
		$('a.nyroModal').nyroModal({
			height: 550
		});
	}
});

function addToFavourites(id, success_phrase, error_phrase)
{
	$(document).ready(function() {
		if ((str = $.cookie("favourites")) != null) {
			favourites_array = PHPSerializer.unserialize(str);
		} else {
			var favourites_array = new Array();
		}
	
		if (in_array(id, favourites_array) === false) {
			favourites_array.push(id);
			alert(success_phrase);
		} else {
			alert(error_phrase);
		}
	
		$.cookie("favourites", PHPSerializer.serialize(favourites_array), {expires: 365, path: "/"});
	});
}

function ajax_loader_show(msg)
{
	if (msg == null)
		msg = 'Loading...';
	$("#ajax_loader").dialog({
		title: msg,
		resizable: false,
		draggable: false,
		autoOpen: false,
		modal: true,
		closeOnEscape: false,
		width: 250,
		minHeight: 30,
		open: function(event, ui) {$(".ui-dialog-titlebar-close").hide();}
	});
	$("#ajax_loader").dialog('open');
}

function ajax_loader_hide()
{
	$("#ajax_loader").dialog('close');
}

function parseScript(_source) {
	var source = _source;

	// Strip out tags
	while(source.indexOf("<" + "script") > -1 || source.indexOf("<" + "/script") > -1) {
							var s = source.indexOf("<" + "script");
							var s_e = source.indexOf(">", s);
							var e = source.indexOf("<" + "/script", s);
							var e_e = source.indexOf(">", e);
				 
							// Add to scripts array
							scripts.push(source.substring(s_e+1, e));
							// Strip from source
							source = source.substring(0, s) + source.substring(e_e+1);
	}

						// Return the cleaned source
						return source;
}

function evalScripts() {
	// Loop through every script collected and eval it
	for(var i=0; i<scripts.length; i++) {
		try {
			eval(scripts[i]);
		}
		catch(ex) {
			// do what you want here when a script fails
		}
	}
}

function seo_write(cat_name)
{
	var connect_symbol = "-";
    var str = "";
    var i;
    var exp_reg = new RegExp("[a-zA-Z0-9-]");
    var exp_reg2 = new RegExp("[ ]");
    cat_name.toString();
    for (i=0 ; i < cat_name.length; i++) {
        if (exp_reg.test(cat_name.charAt(i))) {
            str = str+cat_name.charAt(i);
        } else {
            if (exp_reg2.test(cat_name.charAt(i))) {
                if (str.charAt(str.length-1) != connect_symbol) {
                    str = str+connect_symbol;
                }
            }
        }
    }
    if (str.charAt(str.length-1) == connect_symbol)
        str = str.substr(0, str.length-1);
    return str;
}

function seo_name_write(cat_name)
{
    $('#seo_name').val(seo_write(cat_name));
    return false;
}

function in_array(val, arr) 
{
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] == val)
			return i;
	}
	return false;
}

function urlencode (str) {
    // URL-encodes string  
    // 
    // version: 911.718
    // discuss at: http://phpjs.org/functions/urlencode    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior    // %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    str = (str+'').toString();
        // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

function isset(varname)  {
  if(typeof( window[ varname ] ) != "undefined") return true;
  else return false;
}
