/*
 * ===============================
 * AutoPortal CMS
 * -------------------------------
 * Developed by Dmitriy Ulyanov
 * -------------------------------
 * Copyright (c) 2009
 * ===============================
 */

function getAbsoluteLocation() {

    var re1 = new RegExp(/\/[^\/]+$/g)
    return location.protocol + '//' + location.hostname + location.pathname.replace(re1, '');

}

var locationAbsolute = getAbsoluteLocation();
var locationBase = location.protocol + '//' + location.hostname;

function updateCitiesByRegion(region_id) {
    $(document).ready(function(){
            $("select#city_id").empty();
            $("select#city_id").append($("<option value=''>---</option>"));
            $("select#city_id").removeAttr('disabled');

            $.ajax({
                    type: "GET",
                    url: locationBase + "/index.php",
                    cache: false,
                    data: "show=user&act=ajax&do=updateCitiesByRegion&region_id=" + region_id,
                    dataType: 'json',
                    beforeSend: function() {
                            $("#wait").show();
                    },
                    success: function(data, status) {
                        for (var i = 0; i < data.cities.length; i++) {
                                $("select#city_id").append($("<option value='"+data.cities[i].city_id+"'>"+data.cities[i].name+"</option>") );
                        }
                        $("#wait").hide();
                    },
                    error: function(data, status, e){
                        $("#wait").hide();
                    }
            });
    });
}

function updatePerPage(per_page, baseUrl) {
	var get = location.search;
	var url = 'http://'+location.hostname+baseUrl+'?';
	var param = {};

	if(get != '') {
		tmp = (get.substr(1)).split('&');
		for(var i=0; i < tmp.length; i++) {
			tmp2 = tmp[i].split('=');
			param[tmp2[0]] = tmp2[1];
		}

		for (var key in param) {
			if (key != 'page' && key != 'per_page') url += key+"="+param[key]+"&";
		}
	}
	top.location.href = url+'per_page='+per_page;
}

function checkCaptcha(inputId, submitId, waitId, doNotLockSubmit) {

    if ($("#"+inputId).val() == '') {
        alert('Введите проверочный код!');
        $("#"+submitId).removeAttr('disabled');
        return false;
    }

    codePassed = 0;

    var request = $.ajax({
            type: "GET",
            url: locationBase + "/index.php",
            cache: false,
            data: "show=ajax&do=checkCaptcha&code=" + $("#"+inputId).val(),
            dataType: 'json',
            async: false,
            beforeSend: function() {
                    if (doNotLockSubmit != true)
                        $("#"+submitId).attr('disabled', 'disabled');
                    $("#"+waitId).show();
            },
            success: function(data, status) {
                if (data.result == 'FAIL') {
                    alert('Неверно введен проверочный код!');
                    $("#"+submitId).removeAttr('disabled');
                    $("#"+waitId).hide();
                    return false;
                } else if (data.result == 'OK') {
                    $("#"+submitId).removeAttr('disabled');
                    $("#"+waitId).hide();
                    codePassed = 1;
                    return true;
                }
            },
            error: function(data, status, e){
                $("#"+waitId).hide();
                $("#"+submitId).removeAttr('disabled');
                return false;
            }
    });

    if (codePassed) return true;
    else return false;

}

function getRadioGroupValue(radioGroupObj){
	for (var i=0; i < radioGroupObj.length; i++)
	    if (radioGroupObj[i].checked) return radioGroupObj[i].value;
	   return null;
}

$(document).ready(function () {
    $('#wrap > #peoples').children('div').hover(
        function () {
            var shadow = $(this).children('b');
            $(this).children('a').children('img').animate({ "top": "7px", "left": "7px", "width": "60px", "height" : "60px" }, 200);
            $(this).children('img.p_shadow').animate({ "top": "0", "left": "0", "width": "74px", "height": "74px" }, 200);
    },
        function () {
            $(this).children('img.p_shadow').animate({ "width": "32px", "height": "32px", "top": "30px", "left": "20px" }, 200);
            $(this).children('a').children('img').animate({ "top": "30px", "left": "20px", "height": "32px", "width" : "32px" }, 200);
        }
     );

     $('#reg_link').click(function() {
         $('#regtypes:visible').length != 1 ? $('#regtypes').show() : $('#regtypes').hide();
         return false;
     });

    $(document).click(function(e){
        if ($(e.target).parents().filter('#regtypes:visible').length != 1) {
            $('#regtypes').hide();
        }
    });

    $('#auto_rubrics, #auto_rubrics_list').mouseover(function() {
            $('#auto_rubrics_list').show();
    });
    $('#auto_rubrics, #auto_rubrics_list').mouseout(function() {
        $('#auto_rubrics_list').hide();
    });


});
