// JavaScript Document

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function itemLoadCallbackFunction(carousel, state)
{
    for (var i = carousel.first; i <= carousel.last; i++) {
        // Check if the item already exists
        if (!carousel.has(i)) {
            // Add the item
            carousel.add(i, "I'm item #" + i);
        }
    }
};

function fb_resize(w, h) {
  w = parseInt(w);
  h = parseInt(h);
  if (w > 0 || h > 0) {
    if (w > 0) {
        $("#fancybox-wrap").width(w);
        //$("#fancybox-inner").width(w);
        $("#fancybox-content").width(w);
    }
    if (h > 0) {
        $("#fancybox-wrap").height(h);
        //jQuery("#fancybox-inner").height(h);
        $("#fancybox-content").height(h);
    }
    $.fancybox.resize();
  }
}

jQuery(document).ready(function() {
	
$(".storeimage").tooltip({ 	
	position:	'top right'
});

$(".price-extended-point").tooltip({
	position:	'bottom left'
});

$("a[rel=loginform]").tooltip({
	effect: 'toggle',
	//tipClass: 'loginform',
	tip: '#loginform',
	position: 'bottom left',
	offset:	[-4, 290],
	onBeforeShow: function() {
		var tooltip = this.getTip();
		var link = this.getTrigger();
		tooltip.find('input[name=mybackurl]').val( link.attr('href') );
	}
});

$("a[rel=loginform]").click(function() {
  return false;
});
	
$("div.titles", this).click(function() {			
		$(this).toggleClass('current');			
		$(this).next().slideToggle("fast");	
		$('.groupsum > div', this).toggle();
});


$("div.ordertitle", this).click(function(event) {
	var $target = $(event.target);
	if (!($target.hasClass('btn'))) {	
	$(this).toggleClass('current');	
	$(this).next().slideToggle("fast");
	}
});

$("#switchwidth").click(function () {
	  //setCookie
	  if ( $("#globalwidth").hasClass("wide") ) {
		  setCookie('askaron_window_width', 'normal', 0, '/');
	  } else {
		  setCookie('askaron_window_width', 'wide', 0, '/');
	  }

      $("#globalwidth").toggleClass("wide");

	  $("#wideon").toggle();
	  $("#wideoff").toggle();
	  $("#fixedtableheader0").unbind();
	  $("#fixedtableheader0").remove();
	  $(".storeitems").fixedtableheader();
    });
	
$(".storeitems").fixedtableheader({
	highlightrow: true, 
    highlightclass: 'hover'
   }); 

$("#feedback").fancybox({
	'width'				: 500,
	'padding'			: 0,
	'autoScale'			: true,
	'transitionIn'		: 'elastic',
	'transitionOut'		: 'elastic',
	'type'				: 'iframe',
	'overlayColor'		: '#fff',
	'overlayOpacity'	: 0.6,
	'titleShow'			: false,
	'showNavArrows'     : false
});

	
// Main menu

$(".mmenu .item").bind("mouseenter",function(){
       $(this).addClass('mia');	  
       }).bind("mouseleave",function(){
       $(this).removeClass('mia');	   
});

$(".mmenu .last").bind("mouseenter",function(){
       $(this).addClass('mla');	  
       }).bind("mouseleave",function(){
       $(this).removeClass('mla');	   
});

$(".mmenu .first").bind("mouseenter",function(){
       $(this).addClass('mfa');	  
       }).bind("mouseleave",function(){
       $(this).removeClass('mfa');	   
});

// Slideshow
$(".slidetabs").tabs(".slide", {
	effect: 'fade',
	fadeOutSpeed: "fast",
	rotate: true
}).slideshow({
	autoplay:true,
	autopause: false,
	interval:4500,
	clickable: false
	});

// News	
$('#anews').jcarousel({
        itemLoadCallback: itemLoadCallbackFunction,
		scroll:	1,
		wrap: "last"		
    });
	
// News	
$('#brandsmenu').jcarousel({
        itemLoadCallback: itemLoadCallbackFunction,
		scroll:	1,
		wrap: "last"		
    });
	
// Contacts	

$(".contacts").tabs( ".contacts div.pane", 
	{
		tabs: 'h2',
		effect: 'fade', // важно
		initialIndex: null,
		onClick: function() {
			var accordeon = this;
			var pane = accordeon.getCurrentPane();
			var index = accordeon.getIndex();

			$(pane).find('div.map_id').each(function(i){
				var code = '';
				code = $(this).html();
				//alert(code);
				if (code != '') {
					if (typeof(window.GLOBAL_arMapObjects[code]) != "undefined" ) {						
						window.GLOBAL_arMapObjects[code].redraw();
						//$(this).find('div.YMaps-b-balloon-content').css('width', '200px');

						pane.find('div.YMaps-b-balloon-content').each(function(i){
							$(this).css('width', '100px');
						});
					}
				}
			});

			var tab = accordeon.getCurrentTab();
			tab.bind('click.myEvents', function() {
				tab.unbind('click.myEvents');
				accordeon.click(0);				
			});

			this.getTabs().each(function(i){
				if ( i!=index ) {
					$(this).unbind('click.myEvents');
				}
			});
		}
	}
);

$(".contacttabs").tabs(".contactpanes > div",
	{ history: true,
		effect: 'fade',
		initialIndex: 0,
		onClick: function() {

			var pane = this.getCurrentPane();
			$(pane).find('div.map_id').each(function(i){
				var code = '';
				code = $(this).html();
				//alert(code);
				if (code != '') {
					if (typeof(window.GLOBAL_arMapObjects[code]) != "undefined" ) {
						window.GLOBAL_arMapObjects[code].redraw();
					}
				}
			});

			pane.find('div.YMaps-b-balloon-content').each(function(i){
				$(this).css('width', '100px');
			});

		}
	});
	

});
