$(document).ready(function() {

    //if ($('div.content-area-left').height() > $('div.content-area').height()) $('div.content-area').height($('div.content-area-left').height());
    $("#email").click(function() {
        if ($(this).attr("value") == 'e-mail') $(this).attr("value", "");
    });
    $("#email").blur(function() {
        if ($(this).attr("value") == '') $(this).attr("value", "e-mail");
    });


    //onlines
    
    //getting contents and creating popup window
    $('.order').click(
      function() {
        if ($(".online-popup").length > 0) {
          $(".online-popup").remove();
        }
        $.get($(this).attr('rel'), function(data) {
          c = jQuery(data);
          c.css('top',($(window).scrollTop()+($(window).height()/2))+"px");
          $('#ffxi').prepend(c);
          makeLiveInteger(c.children().find('input[type="text"]'))
          c.show();
        });
        return false;
      }
    );
    
    //closing online-popup
    $('.o-close').live('click', function() {
      $(this).parents('.online-popup').fadeOut().remove();
      return false;
    });
    
    $('.online-popup .closer').live('click', function() {
      $(this).parents('.online-popup').fadeOut().remove();
      return false;
    });


    //replace integer inputs
    $('input.integer').each(
      function() {
        makeInteger($(this))
      }
    )
    
    //up arrow
    $('span.i-up').live('click', function() {
        value = $(this).prev('input')
        if (isNaN(parseInt(value.val(),10)) == true) {
          value.val(1);
	value.change();
        }
        else
        {
          if (parseInt(value.val(),10) < 100) {
            value.val(parseInt(value.val(),10)+1)
	value.change();
          }
        }
        
      }    
    )
    
    //down arrow
    $('span.i-down').live('click', function() {
        value = $(this).prev().prev()
        if (isNaN(parseInt(value.val(),10)) == true) {
          value.val(0)
	value.change();
        }
        else
        {
          if (parseInt(value.val(),10) > 0)
          {
            value.val(parseInt(value.val(),10)-1)
	value.change();
          }
        }
        
      }    
    )
    
    //check values at .integer
    $('input.integer').blur(
      function() {
        if (isNaN(parseInt($(this).val(),10))) {
          $(this).val(0)
        }
      }
    )

    try
    {
        $('form.trnsfrm').jqTransform({
            imgPath: '/f/i/'
        });
    }
    catch(err)
    {

        }

    try
    {
        if (typeof(bugResize) != 'undefined')
        {
            if (bugResize == 1)
            {
                try
                {
                    $("div.br_holder").find("div.jqTransformSelectWrapper").css('width', '207px');
                    $("div.br_holder").find("div.jqTransformSelectWrapper div span").css('width', '183px');
                    $("div.br_holder").find("div.jqTransformSelectWrapper ul").css('width', '205px');
                    $("div.br_holder").find("div.jqTransformSelectWrapper select").css('width', '185px');
                }
                catch(e) {}
            }
        }
    }
    catch(e)
    {}

    try
    {
        if (typeof(checkRightCol) != 'undefined')
        {
            if (checkRightCol == 1)
            {
                try
                {
                    ht = 170;
                    ht = ht + $('.product div.packings').height()
                    ht = ht + $('.product div.weight').height()
                    ht = ht + $('.product div.online').height()
                    if (ht >= $('.product').height())
                    {
                        $('.product').height(ht)
                    }
                }
                catch(e) {}
            }
        }
    }
    catch(e)
    {}

    try
    {
        if (typeof(NeedTagResize) != 'undefined')
        {
            if (NeedTagResize == 1)
            {
                try
                {
                    if (($('#phototagsx').height() + 210) > $('.content-mainpart').height())
                    {
                        $('.content-mainpart').height(($('#phototagsx').height() + 230));
                    }
                }
                catch(e) {}
            }
        }
    }
    catch(e)
    {}


    try
    {
        if (typeof(innerPage) != 'undefined')
        {
            if (innerPage == 1)
            {
                
                try
                {
                    if (($('#lmh').height() + 190) > $('#mainframe').height())
                    {
                        $('#mainframe').height(($('#lmh').height() + 195));
                    }
                }
                catch(e) {}
            }
        }
    }
    catch(e)
    {}

    $("a.breeder_link").toggle(
    function() {
        var x = $(this).attr('id').substr(3);
        $('#ffxi').height($('#ffxi').height() + $('#contract_table_' + x).height());
        $('#contract_table_' + x).slideDown();
    },
    function() {
        var x = $(this).attr('id').substr(3);
        $('#contract_table_' + x).slideUp();
        $('#ffxi').height($('#ffxi').height() - $('#contract_table_' + x).height());
    }
    );


    //widget
    try
    {
        if (typeof(widgetInit) != 'undefined')
        {
            if (widgetInit == true)
            {
                var currentScore = parseInt($('#warning5').attr('rel'));


                $("a.widgetPic").click(
                function() {

                    if (currentScore <= 5)
                    {
                        if ($(this).hasClass('selected'))
                        {
                            $(this).removeClass('selected');
                            currentScore--;
                            $('#warning5').attr('rel', currentScore);
                            $('.itemAdder').css('visibility', 'hidden');
                        }
                        else
                        {
                            if (currentScore < 5)
                            {
                                $(this).addClass('selected');
                                currentScore++;
                                $('#warning5').attr('rel', currentScore);
                                $('.itemAdder').css('visibility', 'visible');
                            }
                            else $('#warning5').effect("highlight", {},
                            3000);
                        }
                    }
                    else
                    {
                        if ($(this).hasClass('selected'))
                        {
                            $(this).removeClass('selected');
                            currentScore--;
                            $('#warning5').attr('rel', currentScore);
                            $('#itemAdder').css('visibility', 'hidden');
                        }
                        else
                        {
                            $('#warning5').effect("highlight", {},
                            3000);
                        }
                    }
                    //if ((currentScore > 0) && (currentScore < 6)) $('#itemAdder').css('visibility','visible'); else $('#itemAdder').css('visibility','hidden');
                    return false;
                }
                );



                $("#addedItems li span.x").live("click",
                function()
                {
                    $(this).parent().remove();
                    currentScore--;
                    $('#warning5').attr('rel', currentScore);
                }
                );

                $('div.wideRuler span').draggable({
                    axis: "x",
                    containment: "parent",
                    stop: function(event, ui) {
                        $('#sizeWidth').attr('value', parseInt($("#xox").css('left')) + 150);
                    }
                });

                $('#sizeWidth').change(function() {
                    if (parseInt($(this).attr('value')) < 150) $(this).attr('value', 150);
                    if (parseInt($(this).attr('value')) > 500) $(this).attr('value', 500);
                    var newPos = parseInt($(this).attr('value') - 150);
                    $("#xox").css('left', newPos + 'px');
                });
            }
        }
    } catch(e)
    {}

    try
    {
        if (typeof(NeedMenuResize) != 'undefined')
        {
            if (NeedMenuResize == 1)
            {


                var lmh = parseInt($('#lmh div.menu').height()) + 100;

                var dfg = $('#main_p').height() + 438 + 75;
                var dmg = lmh + 120;
                // 250

                if (dfg > dmg)
                {
                    $('#ffxi').height(dfg);
                    // 370
                }
                else
                {
                    $('#ffxi').height(dmg);
                    // 170
                }

            }
        }
    }
    catch(e) {}

	/*History*/
	$(".h-review-popup").hide();
	$(".review_link a").not(".review_link_show a").bind("click", function() {
		$('.h-review-popup #order_num').text($(this).attr('ord'));
		$('.h-review-popup #order_date').text($(this).attr('ord_date'));
		$('.h-review-popup input[name="order_id"]').val($(this).attr('ord'));
	    $(".h-review-popup").show();
		return false;
	});
	$(".rp-close").bind("click", function() {
	    $(this).parents(".h-review-popup").hide();
	});
	$(".b-rp").hide();
	$(".review_link_show a").bind("click", function() {
	    $(this).parents(".review_link_show").toggleClass("review_link_hide");
		if($(this).parents(".review_link_show").hasClass("review_link_hide")){
            $(this).html("Скрыть отзыв");
			$("#" + $(this).attr("href").slice(1)).show();
            }
        else{
            $(this).html("Показать отзыв");
			$("#" + $(this).attr("href").slice(1)).hide();
            };
		return false;
	})


$(".ht1-popup-wr .big a")
.bind("mouseenter", function() {
$(this).parents(".ht1-popup-wr ").addClass("ht1-popup-open");
})
.bind("mouseleave", function() {
$(".ht1-popup-open").removeClass("ht1-popup-open");
});

});

jQuery(function($) {
    $('#top10scroll').serialScroll({
        target: '#t10-items',
        items: 'li',
        prev: '#prev',
        next: '#next',
        duration: 200,
        cycle: false
    });
});


function makeInteger(e) {
  d = jQuery('<div class="integer-input"></div>');
  e.wrap(d);
  e.attr('maxlength','2')
  spans = jQuery('<span class="i-up"></span><span class="i-down"></span>');
  e.after(spans);
  if (isNaN(parseInt(e.val(),10)) == true) {
    e.val(0)
  }
}

function makeLiveInteger(e) {
  p = e.parent();
  p.removeClass('numb').addClass('integer-input')
  e.attr('maxlength','2')
  spans = jQuery('<span class="i-up"></span><span class="i-down"></span>');
  e.after(spans);
  if (isNaN(parseInt(e.val(),10)) == true) {
    e.val(0)
  }
}

