var numdlg = 1;
function details(id, obj) {
	$.post('/index.php', {loadAjaxService: 'Stuffs', Method: 'getDetails', id: id}, function(data){
		$('<div class="highslide-html-content" id="div'+numdlg+'"></div>').appendTo('body');
        $('#div' + numdlg).html(data);
        var ret = hs.htmlExpand(obj, {contentId: 'div' + numdlg});
		$('.highslide-html').css({border: '1px solid #000000'});
		return ret;
	});
}
hs.Expander.prototype.onAfterClose = function() {
	//this.close();
	$('#div' + numdlg).remove();
	numdlg = numdlg + 1;
}
function getZakazForm(){
	$('#zakaz').html('');
	$.post('/index.php', {loadAjaxService: 'Feedback', Method: 'getFeedback'}, function(data){
		$('#zakaz').html(data);
/*
		var time = +new Date();
		$('#fdbForm #captcha').attr('src', $('#fdbForm #captcha').attr('src') + '?' + time);
*/
		$('#fdbForm').validate({
            submitHandler: function(form) {
                var flag = false;
                $(':input', form).each(function() {
                    if ($(this).attr('value') == '')
                        flag = true;
                });
                if (!flag)
                    form.submit();
                else
                    alert('Заполнены не все поля');
            },
            focusInvalid: false,
            focusCleanup: true,
            rules: {
                keystring: {
                    required: true,
                    remote: "/get_captcha.php"
                }
            },
            messages: {
                keystring: {
                    required: 'Введите код',
                    remote: 'Неправильный код'
                }
            },
            errorPlacement: function(error, element) {
                var place = element.parent().parent().next('tr').find('div');
                $(element).parent().parent().next('tr').css({display: 'block'});
                $(place).css({color: '#FF7070', fontSize: '10px'});
                $(place).html(error);
            }
        });
	});
}
function getSize() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];
}

$(document).ready(function() {
	$(document).pngFix();
    HSInit();
    var w = 0;
    $('#top_menu .links li').each(function() {
        w = w + $(this).attr('offsetWidth');
    });
    if (w > 0) {
        $('#top_menu').css({width: w + 'px'});
    }
    $('.projects .list li').hover(function() {
        $(this).css({background: 'transparent url(/Images/project_bg.png) top left no-repeat'});
        $(this).pngFix();
    },
    function() {
        $(this).css({background: 'transparent'});
    });
    var dim = getSize();
    if ($('.content_wrap').attr('offsetHeight') < dim[1]) {
        var h = dim[1] - $('.content_wrap').attr('offsetHeight');
//        $('#content').css({height: h + 'px'});
    }
	getZakazForm();
	$('#registerFormLoad').load(function() {
		var ret = null;
		ret = $(this.contentWindow.document.body).html();
		$(this.contentWindow.document.body).html('');
		if (ret == 'true') {
			alert('Спасибо за Ваш запрос! Мы с Вами свяжемся в кратчайшие сроки!');
       		getZakazForm();
		}
		else
            if (ret == 'false')
			     alert('Ошибка! Попробуйте еще раз!');
	});
});
function HSInit() {
    hs.graphicsDir = '/Modules/Js/highslide/graphics/';
    hs.expandCursor = 'zoomin.cur'; // null disables
    hs.restoreCursor = 'zoomout.cur'; // null disables
    hs.expandDuration = 250; // milliseconds
    hs.restoreDuration = 250;
    hs.marginLeft = 15;
    hs.marginRight = 15;
    hs.marginTop = 15;
    hs.marginBottom = 15;
    hs.zIndexCounter = 1001; // adjust to other absolutely positioned elements
    hs.loadingOpacity = 0.75;
    hs.allowMultipleInstances= false;
    hs.numberOfImagesToPreload = 5;
    hs.outlineWhileAnimating = 2; // 0 = never, 1 = always, 2 = HTML only
    hs.outlineStartOffset = 3; // ends at 10
    hs.padToMinWidth = false;
    hs.fullExpandPosition = 'bottom right';
    hs.align = 'center';
    hs.transitions = ['expand', 'crossfade'];
    hs.fadeInOut = true;
    hs.lang = {cssDirection:'ltr', loadingText : 'Загрузка...', loadingTitle : 'Клик для отмены', focusTitle : 'Вывести на ближний план', fullExpandTitle : 'Расширить до реального размера (f)', previousText : '<<', nextText : '>>', moveText : '<b>&#8596;</b>', closeText : '<b>X</b>', closeTitle : 'Закрыть (Esc)', resizeTitle : 'Размер', playText : 'Авторежим', playTitle : 'Слайд-шоу (пробел)', pauseText : 'Пауза', pauseTitle : 'Приостановить слайдшоу (пробел)', previousTitle : 'Назад', nextTitle : 'Вперёд', moveTitle : 'Переместить окно', fullExpandText : 'Полный размер', number: 'Фото %1 of %2', restoreTitle : 'Клик, чтобы закрыть окно. Нажмите и двигайте мышь, чтобы переместить окно. Влево / Вправо – смена изображений.'};
    hs.outlineType = '';
    hs.wrapperClassName = 'dark';
    hs.outlineType = 'rounded-white';
    hs.dimmingOpacity = 0.25;
    hs.showCredits = false;
}
function changePhoto(big_src, mid_src) {
    $('#big_photo').html("<a href='/Upload/Stuff/" + big_src + "' onclick='return hs.expand(this);'><img src='/Upload/Stuff/" + mid_src + "' border='0' alt='' /></a>");
}
function addToCompare(id) {
    $.post('/index.php', {loadAjaxService: 'Basket', Method: 'Compare', productId: id}, function(data) {
        if (Number(data) > 0)
            $('#basket').css({display: 'block', float: 'left'});
    });
}
function addToCart(obj) {
    var cnt = $(obj).prev().attr('value');
    var id = $(obj).prev().attr('name');
    id = id.replace('st_', '');
    $.post('/index.php', {loadAjaxService: 'Basket', Method: 'ToCart', id: id, cnt: cnt}, function(data) {
        var arr = eval('(' + data + ')');
        if (arr['cnt'] > 0) {
            $('#basket').css({display: 'block'});
            $('#basket #count').html(arr['cnt']);
            $('#basket #total').html(arr['total_price'] + ' р');
            if (Number(arr['discount']) > 0)
                $('#basket #discount').css({display: 'block'}).html("Cо скидкой: <span style='color: #ff0000; font-size: 1.1em; font-weight: bold;'>" + arr['total']+ " р.</span>")
            else
                $('#basket #discount').css({display: 'none'});
        }
    });
}
function changeSearch(obj) {
    var group = $(obj).attr('value');
    $.post('/index.php', {loadAjaxService: 'Search', Method: 'GetSearchForm', group: group}, function(data) {
        $('#search_form').html(data);
    });
}
function sklon(size, ed, mn, mn_a) {
    if (size != Math.floor(size))
        return mn;
    if(Math.floor((size % 100) / 10) == 1)
        return mn_a;
    var a = size%10;
    if (a == 2 || a == 3 || a == 4)
        return mn;
	if(a == 1)
		return ed;
	return mn_a;
}
$(document).ready(function(){
    $('#fast_order_button').click(function(){
        return hs.htmlExpand(this, {contentId:'fast-order-form-on'});
    });
});


