// perform JavaScript after the document is scriptable.
$(function() {
	// setup ul.tabs to work as tabs for each div directly under div.panes
	$("ul.tabs").tabs("div.panes > div",{history: true});
	$("ul.tabs li a img[title]").tooltip({
		opacity: 0.9,
		effect: 'slide',
		position: "bottom right",
		offset: [20, -30]
	}).dynamic({bottom: {direction: 'down', bounce: true}});
});
// *******************

function disableAddToCart(elementId, mode, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;
	
	if (mode == "detail")
		$(element).html("<span>" + text + "</span>").toggleClass("disabled")
			.removeAttr("href").unbind('click').css("cursor", "default");
	else if (mode == "list")
		$(element).html(text).removeClass("catalog-item-buy").addClass("catalog-item-in-the-cart")
			.unbind('click').css("cursor", "default");
}

function addToCart(element, imageToFlyId, mode, text)
{
	if (!element || !element.href)
		return;

	var button = $(element);
	if (mode == "detail")
		button.toggleClass("disabled").unbind('click').css("cursor", "default");
	else if (mode == "list")
		button.removeClass("catalog-item-buy").addClass("catalog-item-in-the-cart").unbind('click').css("cursor", "default");

	$.get(
		element.href + "&ajax_buy=1",
		$.proxy(
			function(data) {

				if (this.mode == "detail")
					this.button.removeAttr("href").html("<span>" + text + "</span>");
				else if (this.mode == "list")
					this.button.removeAttr("href").html(text);

				var imageElement = document.getElementById(this.imageToFlyId);
				if (!imageElement)
				{
					$("#cart_line").html(data);
					return;
				}

				var hoverClassName = "";
				var wrapper = null;
				if (this.mode == "detail")
				{
					hoverClassName = "catalog-detail-hover";
					wrapper = this.button.parents("div.catalog-detail");
				}
				else if (this.mode == "list")
				{
					hoverClassName = "catalog-item-hover";
					wrapper = this.button.parents("div.catalog-item");
				}

				wrapper.unbind("mouseover").unbind("mouseout").removeClass(hoverClassName);

				var imageToFly = $(imageElement);
				var position = imageToFly.position();
				var flyImage = imageToFly.clone().insertBefore(imageToFly);

				flyImage.css({"position": "absolute", "left": position.left, "top": position.top});
				flyImage.animate({width: 0, height: 0, left: 948, top: -58}, 600, 'linear');
				flyImage.data("hoverClassName", hoverClassName);
				flyImage.queue($.proxy(function() {

					this.flyImage.remove();
					$("#cart_line").html(data);

					if (this.wrapper.data("adminMode") === true)
					{
						var hoverClassName = "";
						if (this.mode == "detail")
							hoverClassName = "catalog-detail-hover";
						else if (this.mode == "list")
							hoverClassName = "catalog-item-hover";

						this.wrapper.addClass(hoverClassName).bind({
							mouseover: function() {$(this).removeClass(hoverClassName).addClass(hoverClassName);},
							mouseout: function() {$(this).removeClass(hoverClassName);}
						});
					}

				}, {"wrapper" : wrapper, "flyImage" : flyImage, "mode": this.mode}));

			}, {"button": button, "mode": mode, "imageToFlyId" : imageToFlyId}
		)
	);

	return false;
}

function disableAddToCompare(elementId, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;

	$(element)
		.removeClass("catalog-item-compare").addClass("catalog-item-compared")
		.text(text)
		.unbind('click').removeAttr("href")
		.css("cursor", "default");

	return false;
}

function addToCompare(element, text)
{
	if (!element || !element.href) 
		return;

	var href = element.href;
	var button = $(element);

	button.removeClass("catalog-item-compare").addClass("catalog-item-compared").unbind('click').removeAttr("href").css("cursor", "default");

	$.get(
		href + '&ajax_compare=1&backurl=' + decodeURIComponent(window.location.pathname),
		$.proxy(
			function(data) {

				var compare = $("#compare");
				compare.html(data);
				
				this.text(text);

				if (compare.css("display") == "none") {
					compare.css({"display": "block", "height": "0"});
					compare.animate({"height": "22px"}, 300);
				}
			}, button
		)
	);
	
	return false;
}

$(document).ready(function() {	
	
	$('#title-search-input').focus(function() {
		if (this.value == "поиск по сайту") {
			this.value = "";
			$(this).css({'color':'#000'});
		}
	});
	
	$('#title-search-input').blur(function() {
		if (this.value == "") {
			this.value = "поиск по сайту";
			$(this).css({'color':'#ccc'});
		}
	});
	
	$('#srndword').click(function() {
		var word = this.title;
		$('#title-search-input').each(function (obj){
			this.value = word;
			$(this).css({'color':'#000'});
		}); 
	});
	
	$("#software-list").treeview({
		persist: "location",
		collapsed: true,
		unique: true
	});
	
	$("#vendors-list").treeview({
		persist: "location",
		collapsed: true,
		unique: true
	});

	$("#services-list").treeview({
		persist: "location",
		collapsed: false,
		unique: true
	});
	
	$("#virtuemart-list").treeview({
		persist: "location",
		collapsed: true,
		unique: true
	});
	
	$('.splCont').attr('style','display:none; padding:3px 5px;')
    $('.splLink').click(function(){
      $(this).parent().children('div.splCont').toggle('normal');
      return false;
    });
	
    $('A.add2basket').click(function() {
        var cart = '<a href="/personal/cart/">Оформить</a>';
        $.ajax({type:"GET",url:"index.php",data:"action=ADD2BASKET&ajax_buy=1&id="+this.id});
        $.fancybox('<b>Товар добавлен в корзину!</b><div align="center">'+cart+'</div>');
    });
    
        
        $('A#call-of-the-manager').fancybox({
            'titlePosition'	: 'inside',
            //'width'		: '550px',
            //'height'		: '600px',
            'transitionIn'	: 'fade',
            'transitionOut'	: 'fade',
            'hideOnOverlayClick': true,
            'hideOnContentClick': false,
            'overlayShow' 	: true,
            'overlayOpacity'	: 0.8,
            'overlayColor'	: '#3872b2',
            'autoScale'		: false,
            'type'		: 'iframe'
	});
        
        
    /* mibew-popup */
    $('.mibew-popup .title').click(function() {
        $.cookie('my_popup',($.cookie('my_popup')<1?1:0));
        $('.mibew-popup .texts').slideToggle('normal');
        if($.cookie('my_popup')==1){
            $('.mibew-popup').css('opacity','0.45');
        } else {
            $('.mibew-popup').css('opacity','1');
        }
        //console.log($.cookie('my_popup'));
    });
    
    if ($.cookie('my_popup')==null) {
        $('.mibew-popup .texts').css('display','none');
        $('.mibew-popup').css('opacity','0.45');
        $('.mibew-popup .texts').delay(1000).slideToggle('normal',function() {
            $('.mibew-popup').animate({'opacity':'1'},300).animate({'opacity':'0.5'},300).animate({'opacity':'1'},300);
        });
        
    }
    
    $('.mibew-popup .title').mouseover(function(){
        if ($.cookie('my_popup')>0) $('.mibew-popup').css('opacity','1');
    });
    $('.mibew-popup .title').mouseout(function(){
        if ($.cookie('my_popup')>0) $('.mibew-popup').css('opacity','0.45');
    });
    
    if ($.cookie('my_popup')==1) {
        $('.mibew-popup .texts').css('display','none');
        $('.mibew-popup').css('opacity','0.45');
    }
});


