var ajax_fn = '';

$(document).ready(function() 
{
	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", "");
					});
				}
			}
		});
	}

	$(".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);
	});

	$(".img_div, #img_div").each( function() {
		div_width = $(this).width();
		div_height = $(this).height();
		
		img_width = $(this).find("img").width();
		img_height = $(this).find("img").height();
		
		if (div_width < img_width) {
			$(this).find("img").width(div_width);
		} else {
			if (div_height < img_height) {
				$(this).find("img").height(div_height);
			}
		}
	});
	
	$("a.ancLinks").click(function () {
		elementClick = $(this).attr("href");
		destination = $(elementClick).offset().top;
		$("html").animate({ scrollTop: destination}, 1100 );
		return false;
    });
});

function addToFavourites(id, success_phrase, error_phrase)
{
	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 = '';
	tb_show(msg, '#TB_inline?height=24&width=220&inlineId=ajax_loader');
}

function ajax_loader_hide()
{
	tb_remove();
}

function seo_write(cat_name)
{
    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) != "_") {
                    str = str+"_";
                }
            }
        }
    }
    if (str.charAt(str.length-1) == "_")
        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, '+');
}