$(document).ready(function(){
	$('.highlight').each(function(){
		$(this).mouseover(function(){
			$(this).parent().css('background','url(/img/menuBg.gif) top left no-repeat');
		});
		
		$(this).mouseout(function(){
			$(this).parent().css('background','none');
		});
	});
});

function delPostTitle(id) {
    $('#box'+id).parent().parent().parent().prev().hide();
}

function removeFromBox(id,evt) {
    $.ajax({
        method: "get",
        url: 'http://'+location.host+'/callback/removeFromBox/'+id+'/',
        dataType:"json",
        cache:false,
        success: function(data){
            $('#box').html('<span>'+data.count+'</span> шт.<span>'+data.price+'</span> руб.');
            $('#allPrice').html(data.price);
            $('#allPriceDost').html(data.priceDost);
        	var messageDiv = $('#message'+id);
        	$(messageDiv).css({'font-weight':'bold','font-size':'15px','text-align':'center','height':'70px','width':'200px','color':'#fff','background':'#5FBE28','zIndex':'1000','position':'absolute'}).html('<br />'+data.message).fadeIn('slow');
        	setTimeout('$("#message'+id+'").fadeOut("slow")', 1500);

            if($('#box'+id).parent().prev().attr('class') == 'parent') {
                if(!$('#box'+id).parent().next().html() || $('#box'+id).parent().next().attr('class') == 'parent') {
                    $('#box'+id).parent().prev().hide();
                }
            }
            
            $('#box'+id).html('');
            return false;
        }
    });
    
    return false;
}

function addToBox(id,evt) {
    if (id == 'postList') {
        $.ajax({
            type: "post",
            url: 'http://'+location.host+'/callback/addToBox/',
            data: $('#postList').serialize(),
            dataType:"json",
            cache:false,
            success: function(data){
                location.href="http://www.selo-lukoshkino.ru/catalog/box/";
                $('#box').html('<span>'+data.count+'</span> шт.<span>'+data.price+'</span> руб.');
                $('#allPrice').html(data.price);
                $('#allPriceDost').html(data.priceDost);            
            	var messageDiv = $('#messagePostList');
            	$(messageDiv).css({'font-weight':'bold','font-size':'15px','text-align':'center','height':'70px','width':'200px','color':'#fff','background':'#5FBE28','zIndex':'1000','position':'absolute'}).html('<br />'+data.message).fadeIn('slow');
            	setTimeout('$("#messagePostList").fadeOut("slow")', 1500);
                return false;
            }
        });
    }
    else {
        $.ajax({
            method: "get",
            url: 'http://'+location.host+'/callback/addToBox/'+id+'/',
            dataType:"json",
            cache:false,
            success: function(data){
                location.href="http://www.selo-lukoshkino.ru/catalog/box/";
                $('#box').html('<span>'+data.count+'</span> шт.<span>'+data.price+'</span> руб.');
                $('#allPrice').html(data.price);
                $('#allPriceDost').html(data.priceDost);            
            	var messageDiv = $('#message'+id);
            	$(messageDiv).css({'font-weight':'bold','font-size':'15px','text-align':'center','height':'70px','width':'200px','color':'#fff','background':'#5FBE28','zIndex':'1000','position':'absolute'}).html('<br />'+data.message).fadeIn('slow');
            	setTimeout('$("#message'+id+'").fadeOut("slow")', 1500);
                return false;
            }
        });
    }
    
    return false;
}

function changeItemBox(id) {
    var count = $('#itemCount'+id).val()*1;
    if(count < 1) {
        count = 1;
        $('#itemCount'+id).val(count);
    }
    $.ajax({
        method: "get",
        url: 'http://'+location.host+'/callback/changeItemBox/'+id+'/'+count+'/',
        dataType:"json",
        cache:false,
        success: function(data){
            $('#priceSpan'+id).html(data.itemPrice+' руб.');
            $('#box').html('<span>'+data.count+'</span> шт.<span>'+data.price+'</span> руб.');
            $('#allPrice').html(data.price);
            $('#allPriceDost').html(data.priceDost);

            return false;
        }
    });

    return false;
}

function plusItemBox(id) {
    $.ajax({
        method: "get",
        url: 'http://'+location.host+'/callback/plusItemBox/'+id+'/',
        dataType:"json",
        cache:false,
        success: function(data){
            $('#priceSpan'+id).html(data.itemPrice+' руб.');
            $('#box').html('<span>'+data.count+'</span> шт.<span>'+data.price+'</span> руб.');
            $('#allPrice').html(data.price);
            $('#allPriceDost').html(data.priceDost);            
            $('#itemCount'+id).val(data.itemCount);
            return false;
        }
    });

    return false;
}

function minusItemBox(id) {
    if($('#itemCount'+id).val()*1 > 1)
        $.ajax({
            method: "get",
            url: 'http://'+location.host+'/callback/minusItemBox/'+id+'/',
            dataType:"json",
            cache:false,
            success: function(data){
                $('#priceSpan'+id).html(data.itemPrice+' руб.');
                $('#box').html('<span>'+data.count+'</span> шт.<span>'+data.price+'</span> руб.');
                $('#allPrice').html(data.price);
                $('#allPriceDost').html(data.priceDost);            
                $('#itemCount'+id).val(data.itemCount);
                return false;
            }
        });
    
    return false;
}

function changeItemPrice(id, price) {
    $.ajax({
        method: "get",
        url: 'http://'+location.host+'/callback/editBox/'+id+'/'+price+'/',
        dataType:"json",
        cache:false,
        success: function(data){
            $('#priceSpan'+id).html(data.itemPrice+' руб.');
            $('#box').html('<span>'+data.count+'</span> шт.<span>'+data.price+'</span> руб.');
            $('#allPrice').html(data.price);
            $('#allPriceDost').html(data.priceDost);
        }
    });
}

function saveText(id) {
    var text = $('#more'+id).val();
    $.ajax({
        type: "post",
        data: "text="+text,
        url: 'http://'+location.host+'/callback/saveText/'+id+'/',
        dataType:"json",
        cache:false
    });
}

var curMonth = 0;
function setHolidays() {
    $.ajax({
        method: "get",
        url: 'http://'+location.host+'/holidaies/getHolidaies/'+curMonth+'/',
        dataType:"json",
        cache:true,
        success: function(data){
            if (data['current'] && !$('#holidayAnnounce').html()) {
                $('#holidayAnnounce').html('<p><img src="/img/image.gif" alt="" />Через '+data['current']['last']+' - <a href="/holidaies/show/'+data['current']['id']+'/">'+data['current']['info']+'</a></p><p><a href="/holidaies/show/'+data['current']['id']+'/">Что стоит купить в подарок?</a></p>');
            }
            $("#datepicker").find('.ui-state-default').each(function(){
                var day = $(this).html();
                //+'<br>'+data[day]['text']
                if (data[day]) {
                    $(this).parent().attr('onclick','return true;');
                    $(this).attr('href','/holidaies/show/'+data[day]['id']+'/').attr('onclick','return true;');
                    $(this).html('<span style="color:red;" id="holiday'+day+'a" onmouseout="hideHolidayInfo('+day+');" onmouseover="showHolidayInfo('+day+', \''+data[day]['name']+'\');"><b>'+day+'</b></span>');
                }
            });
        }
    });
}

function showHolidayInfo(day, info) {$('#holiday'+day+'a').append('<div id="holiday'+day+'" style="padding:10px; color:#000; background-color:#89D85B; position:absolute;">'+info+'</div>');}
function hideHolidayInfo(day) {$('#holiday'+day).remove();}

$(function() {
//	$('.map').maphilight();
//	$('#object1').cluetip({topOffset:100, leftOffset:130, activation:'focus', positionBy:'fixed', ajaxCache:true, sticky:true, closePosition:'title', arrows:true});
//	$('#object2').cluetip({topOffset:200, leftOffset:370, activation:'focus', positionBy:'fixed', ajaxCache:true, sticky:true, closePosition:'title', arrows:true});
//	$('#object3').cluetip({topOffset:320, leftOffset:433, activation:'focus', positionBy:'fixed', ajaxCache:true, sticky:true, closePosition:'title', arrows:true});
//	$('#object4').cluetip({topOffset:320, leftOffset:510, activation:'focus', positionBy:'fixed', ajaxCache:true, sticky:true, closePosition:'title', arrows:true});
//	$('#object5').cluetip({topOffset:280, leftOffset:580, activation:'focus', positionBy:'fixed', ajaxCache:true, sticky:true, closePosition:'title', arrows:true});
	$('.mapObjects').cluetip({splitTitle:'|', height: '187px', dropShadow:false, clickThrough:true});
    $('#object5').cluetip({topOffset:'-50px', leftOffset:'-150px', splitTitle:'|', height: '187px', dropShadow:false, clickThrough:true});
    
    //positionBy:'fixed',
    $("#datepicker").datepicker({onChangeMonthYear: function(year, month, inst){curMonth = month;}});
});

function showTip(id) {$('#object'+id).focus();}

//onmouseover="$('#li2').css('background','url(img/menuBg.gif) top left no-repeat')" onmouseout="$('#li2').css('background','none')"

function sortItems(type, value, parent) {
    if (value == 'orderList') {
        value = ($('#'+value).val() != 0) ? $('#'+value).val()+'|0' : 'articule|0';   
    }
    if (value == 'orderListDesc') {
        value = ($('#orderList').val() != 0) ? $('#orderList').val()+'|1' : 'articule|1';
        $('#ascDesc').removeAttr('onclick');
        $('#ascDesc').unbind('click');
        $('#ascDesc').bind('click',function(){return sortItems('order', 'orderListAsc', parent);});
        $('#ascDesc').html('&darr;');
    }
    if (value == 'orderListAsc') {
        value = ($('#orderList').val() != 0) ? $('#orderList').val()+'|0' : 'articule|0'; 
        $('#ascDesc').removeAttr('onclick');
        $('#ascDesc').unbind('click');
        $('#ascDesc').bind('click',function(){return sortItems('order', 'orderListDesc', parent);});
        $('#ascDesc').html('&uarr;');
    }
    else {
        $('#priceSort').find('a').each(function(){
            var html = $(this).html().replace('<b>','');
            html = html.replace('</b>','');
            $(this).html(html);
        });
        $('#price'+value).html('<b>до '+value+'</b>');
    }

	$.ajax({
	    url: 'http://'+location.host+'/catalog/sortItems/'+type+'/'+value+'/'+parent+'/',
	    dataType:"json",
	    cache:true,
	    success: function(data){
	        $('#info').fadeOut('fast',function(){
	            $('#paginatorContainer').html(data['paginator']);
                $('#dopnavig').html(data['paginator']);

                $('#info').html('');
                var i = 0; var html = '<table><tr>';
    	        for (key in data['items']) {
    	            if(i != 0 && i%3 == 0) var delimiter = '</tr><tr><td valign="top">';
                    else var delimiter = '<td valign="top">';
    	            if(data['items'][key].id) {
    	                if(data['items'][key].file.exist) {
    	                    var img = '<a rel="imagebox" href="/files/1/600/'+data['items'][key].file.id+'.'+data['items'][key].file.ext+'">';
                        }
                        else var img = '';
                        if (data['items'][key].newItem*1) img += '<img class="img2" src="/img/new.gif"><img onmouseover="showInfo('+data['items'][key].id+');" onmouseout="hideInfo('+data['items'][key].id+');" class="img1" width="186" src="/files/1/186x204/'+data['items'][key].file.id+'.'+data['items'][key].file.ext+'" />';
                        else img += '<img onmouseover="showInfo('+data['items'][key].id+');" onmouseout="hideInfo('+data['items'][key].id+');" width="186" src="/files/1/186x204/'+data['items'][key].file.id+'.'+data['items'][key].file.ext+'" />';
                        if(data['items'][key].file.exist) {               
                            img += '</a>';
                        }
                        if (data['items'][key].info) img += '<p class="p1"><div id="info'+data['items'][key].id+'" style="padding:10px; color:#000; background-color:#89D85B; display:none; position: absolute;">'+data['items'][key].info+'</div></p>';
                    }
                    else img = '<div style="width:205px; height:241px;">&nbsp;</div>';
                    var price = data['items'][key].price ? data['items'][key].price+' руб.' : 'Не указано';
    	            html += delimiter+img+'<p class="p1"><span>'+data['items'][key].articule+'</span></p><p class="p1"><a href="/item/show/'+data['items'][key].id+'/" ><span>'+data['items'][key].name+'</span></a></p><p class="p1"><span class="price">'+price+'</span></p><p class="p1"><a class="a1" onclick="return addToBox('+data['items'][key].id+', event);" href="/callback/'+data['items'][key].id+'/">&nbsp;&nbsp;Купить&nbsp;</a></p><p class="p1"><div id="message'+data['items'][key].id+'" style="display:none"></div></p></td>';
                    i++;
    	        }
                html += '</tr></table>';
                $('#info').html(html);
                $('#info').fadeIn('fast');
                
            	$('a[@rel*=imagebox]').lightBox({
                    txtImage: 'Изображение',
                    txtOf: 'из',
                    imageBlank: '/img/lightbox/blank.gif',
                	imageLoading: '/img/lightbox/loading.gif',
                	imageBtnClose: '/img/lightbox/close.gif',
                	imageBtnPrev: '/img/lightbox/prev.gif',
                	imageBtnNext: '/img/lightbox/next.gif'
                });
            });

            return false;
	    }
	});
    
    return false;
}

function showInfo(id) {
    $('#info'+id).fadeIn('fast');
}

function hideInfo(id) {
    $('#info'+id).fadeOut('fast');
}

function townSelect(val) {
    $.ajax({
        method: "get",
        url: 'http://'+location.host+'/callback/townSelect/'+val+'/',
        dataType:"json",
        cache:false,
        success: function(data){
            $('#allPriceDost').html(data.price);
            return false;
        }
    });

    return false;
}

function showAdressField() {
    var val = $('#town').val();
    $.ajax({
        method: "get",
        url: 'http://'+location.host+'/callback/townSelect/'+val+'/enable/',
        dataType:"json",
        cache:false,
        success: function(data){
//            if($('#inMagazinePay').attr('checked')) {
//                $('#inMagazinePay').removeAttr('checked');
//                $('#sberPay').attr('checked','true');
//            }
            $('#town').removeAttr('disabled');
            $('#adress').removeAttr('disabled');
            $('#forPerson').removeAttr('disabled');
            $('#forPersonPhone').removeAttr('disabled');
            $('#dostavkaTime_1').removeAttr('disabled');
            $('#dostavkaTime_2').removeAttr('disabled');
//            $('#inMagazine').fadeOut('fast');

            $('#allPriceDost').html(data.price);
            return false;
        }
    });
}

function hideAdressField() {
    var val = $('#town').val();
    $.ajax({
        method: "get",
        url: 'http://'+location.host+'/callback/townSelect/'+val+'/disable/',
        dataType:"json",
        cache:false,
        success: function(data){
            $('#town').attr('disabled','true');
            $('#adress').attr('disabled','true');
            $('#forPerson').attr('disabled','true');
            $('#forPersonPhone').attr('disabled','true');
            $('#dostavkaTime_1').attr('disabled','true');
            $('#dostavkaTime_2').attr('disabled','true');
//            $('#inMagazine').fadeIn('fast');

            $('#allPriceDost').html(data.price);
        }
    });
}

function showPostcardFields(id) {
    $('#more'+id).removeAttr('disabled');
    $('#postcardChoice').fadeIn('fast');
}

function hidePostcardFields(id) {
    $.ajax({
        method: "get",
        url: 'http://'+location.host+'/callback/delPostcard/',
        dataType:"json",
        cache:false,
        success: function(data){
            $('#box').html('<span>'+data.count+'</span> шт.<span>'+data.price+'</span> руб.');
            $('#allPrice').html(data.price);
            $('#allPriceDost').html(data.priceDost);
            
            $('#postcardPlace'+id).html('');
            
            $('#more'+id).attr('disabled','true');
            $('#postcardChoice').fadeOut('fast');
        }
    });
}

function redirectForm(type) {
    //if(type == 6) $('.fastTransInfo').show(); else $('.fastTransInfo').hide();
    //if(type == 15) $('.fastSberCard').show(); else $('.fastSberCard').hide();
    //if(type == 5) $('.sberInfo').show(); else $('.sberInfo').hide();
	$('.description').hide();
	$('#_payment_' + type + '_').show();
	

    if (type == 2) {
        $('#LMI_PAYMENT_AMOUNT').val($('#allPriceDost').html());
        $('#sendForm').attr('action','https://merchant.webmoney.ru/lmi/payment.asp');
        $('#sendForm').removeAttr('target');
    }
    else {
        $('#sendForm').attr('action','/callback/save/');
        $('#sendForm').attr('target','_blank');
    }
}

