// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});

var cart_stat=0;
$(function() {
    // Cart
    var isExpanded = false;

    function cartExpand() {
        $('DIV#cart').animate({'width': 625}, 300).queue(function() {
            $(this).animate({'height': 365}, 300).dequeue();
            $('SPAN.recycle').hide();
        });
        cart_stat=1;
        isExpanded = true;
    };

    function cartCollapse() {
        $('DIV#cart').animate({'height': 58}, 300).queue(function() {
            $(this).animate({'width': 71}, 300).dequeue();
            $('SPAN.recycle').show();
        });
        cart_stat=0;
        isExpanded = false;
    };

    $('A#cart_count').live('click', function() {

        $('DIV#cart').stop(true, true);
        if (isExpanded == false) {
            cartExpand();
        } else {
            cartCollapse();
        };
        return false;
    });

    $('A#cart_collapse').live('click', function() {
        cartCollapse();
        return false;
    });

    // Cart scroll
    $('.cart_body_products').jScrollPane({
        verticalDragMinHeight: 16,
        verticalDragMaxHeight: 16
    });

    $('UL.navigation LI, UL.choice_menu LI').hover(
    function() {
        $('SPAN', this).stop(true, true).fadeIn();
    },
    function() {
        $('SPAN', this).stop(true, true).fadeOut();
    }
    );

    $('INPUT.buy').click(function() {

        var idElSection=$(this).parent('div').parent('td').attr('id');
        if(idElSection){
            $.getJSON("/ajax/add_to_basket.php",{ id: idElSection }, function(data){
                ShowCartComplete(data);
            });
        }

        var idElSection=$(this).parent('li').parent('ul').parent('div').parent('td').attr('id');
        if(idElSection){
            $.getJSON("/ajax/add_to_basket.php",{ id: idElSection }, function(data){
                ShowCartComplete(data);
            });
        }

        var idElElement=$(this).parent('form').children('input[name=id]').val();
        if(idElElement){
            var quantity=$(this).parent('form').children('input[name=quantity]').val();
            $.getJSON("/ajax/add_to_basket.php",{ id: idElElement, quantity:quantity }, function(data){
                ShowCartComplete(data);
            });
        }

        var idElElement=$(this).parent('td').parent('tr').children('td').children('form').children('input[name=id]').val();
        if(idElElement){
            var quantity=$(this).parent('td').parent('tr').children('td').children('form').children('input[name=quantity]').val();
            $.getJSON("/ajax/add_to_basket.php",{ id: idElElement, quantity:quantity }, function(data){
                ShowCartComplete(data);
            });
        }

        return false;
    });

    function ShowCartComplete(obj){
        $('.complete .text_num').text(obj.NUM);
        $('.complete .text_sum').text(obj.SUM);

        var bxajaxid = $('#cart form input[name=bxajaxid]').val();
        jsAjaxUtil.InsertDataToNode('/products/?action=delete&cart_open=Y&id=1&bxajaxid='+bxajaxid, 'comp_'+bxajaxid, true);
        //setTimeout("$('.complete').stop(true, true).fadeIn(1500).animate({opacity: 1}, 3500).fadeOut(1500);",1500);
        $('.complete').stop(true, true).fadeIn(1500).animate({opacity: 1}, 3500).fadeOut(1500);
    }

    //$('.company LABEL').click(function() {
    $('.company LABEL').live('click',function() {
        $('.company LABEL').removeClass('active');
        $(this).addClass('active');
        var fieldValue = $(this).attr('for');
        $('INPUT#delivery').val(fieldValue);
    });

    $('.number .minus').click(function() {
        val = $(this).next('.itog').val();
        if (val != 0){
            $(this).next('.itog').val(val - 1);
        }
        return false;
    });

    $('.number .plus').click(function() {
        val = $(this).prev('.itog').val();
        val++;
        $(this).prev('.itog').val(val);
        return false;
    });

	//styling
	;

	if ($('div.catalog div.tables').length) {
		$('div.catalog').css({overflow: 'visible'}).append('<p style="clear: both; height: 0;"></p>');
	}

	if ( $('li.tvoya_cena').length == 1 ) {
		$('li.tvoya_cena').removeClass('cena2');
	}

	//debug

	// recommend
	$('.recom.inactive').hover(function() {
		$(this).find('.rcmpp').toggle();
	});

	// login window

	var isLoginShown = false;
	var isNotLogged = $('#loginwindow').length;

	var initLoginWindow = function() {
		var targetTop = -10;
		if ($(window).height() < 700) {
			targetTop = $(window).height() - 704;
		}

		if (!isLoginShown && isNotLogged) {
			isLoginShown = true;
			$('<div id="loginoverlay"><p id="lmsg">Загрузка...</p><div id="loginclose"></div></div>').appendTo('body');
			var sprite = new Image();
			$(sprite).bind('load', function() {
				$('#lmsg').remove();
				$('.logbadge:first').addClass('swinging');
				$('#loginwindow').appendTo('#loginoverlay').css({display: 'block'}).delay(200).animate({top: targetTop}, 1000, 'easeOutBounce');
			});
			sprite.src = '/img/logsprites.png';

		} else {
			$('#loginoverlay').css({display: 'block'});
			$('.logbadge:first').addClass('swinging');
			$('#loginwindow').delay(200).animate({top: targetTop}, 1000, 'easeOutBounce');
		}
		return false;
	}

	$('#logtoenter, #logtoremind').bind('click', function() {
		$('#logwrapper').toggleClass('flipped');
		return false;
	});

	$('#loginclose').live('click', function() {
		$('#loginwindow').animate({top: -715}, 200, function() {
			$('#loginoverlay').css({display: 'none'});
			$('.logbadge').removeClass('swinging');
			$('#logwrapper').removeClass('flipped');
		});

	});

	$('.logintxt').each(function() {
		$(this).data('defaultValue', $(this).val());
		$(this).bind('focus blur', function() {
			if ( $(this).val() == $(this).data('defaultValue') ) {
				$(this).val('');
			} else if ( $(this).val() == '' ) {
				$(this).val( $(this).data('defaultValue') );
			}
		});
	});

	if (!$.browser.msie) {
		$('#logwrapper input[type="checkbox"]').each(function() {
			$(this).bind('change', function() {
				if ($(this).is(':checked')) {
					$(this).parent().addClass('checked');
				} else {
					$(this).parent().removeClass('checked');
				}
			}).css({opacity: 0}).parent().addClass('chckbx');

			if ($(this).is(':checked')) {
				$(this).parent().addClass('checked');
			}
		});
	}

	$('a.enter, a.enter_recom').bind('click', initLoginWindow);

	// table hover
	var tableAnimTime = 300;
	var $itemTable = $('table.name_list');

	$itemTable.delegate('td:not(:empty)', 'mouseenter', function() {
		$(this).stop(true, true).animate({opacity: 1}, tableAnimTime);
		$itemTable.find('td').not($(this)).stop(true, true).animate({opacity: .4}, tableAnimTime);
	});

	$itemTable.bind('mouseleave', function() {
		$(this).find('td').animate({opacity :1}, tableAnimTime);
	})


	// rub

	$('li.tvoya_cena span.rub').add('span.cena span.rub').text('').append('<span class="rub-sign">');

	$('div.cart_body').find('span.rub').text('').append('<span class="rub-sign-gr">');

	// sticky submenu

	if ( !$('#prodname').length ) {
		var $menu = $('ul.choice_menu');

		if ($menu.length) {
			var $fantomContainer = $('<div id="stickysubmenu">');
			$fantomContainer.appendTo('body');
			$menu.clone().appendTo($fantomContainer);

			$(window).bind('scroll', function() {

				if ( $(window).scrollTop() > $menu.offset().top) {
					$fantomContainer.fadeIn(300);
				} else {
					$fantomContainer.fadeOut(300);
				}
			});
		}
	} else {
		var $menu = $('ul.proects_list');

		if ($menu.length) {
			var $fantomContainer = $('<div id="stickyitemmenu">');
			$fantomContainer.appendTo('body');
			$menu.clone().appendTo($fantomContainer);
			var sul = $('<ul class="smprcont"></ul>')
			sul.appendTo($fantomContainer);
			var yourPrice = $('li.tvoya_cena').first().clone();
			if ( !yourPrice.length ) {
				yourPrice = $('li.cena2 > span:first-child').clone();
			}
			//$('li.tvoya_cena').first().clone().appendTo(sul).addClass('simprice');
			yourPrice.appendTo(sul).addClass('simprice');
			$('form', '#prodbuyform').clone().appendTo($fantomContainer);
			$('a.back').clone().appendTo($fantomContainer);

			$(window).bind('scroll', function() {

				if ( $(window).scrollTop() > $menu.offset().top) {
					$fantomContainer.fadeIn(300);
				} else {
					$fantomContainer.fadeOut(300);
				}
			});
		}
	}






})
