$(function(){
	
	$().piroBox({
	   	my_speed: 300,
	   	bg_alpha: 0.5,
	   	slideShow : 'false',
	   	slideSpeed : 3,
		close_all : '.piro_close'
	});
	
	var scrollWatcher = function(event) {
  	  var window_scroll_top = $(window).scrollTop();
   	    if(window_scroll_top > 244) {
    	  $('.catalog_top_menu').addClass('catalog_top_menu_scroll');
        }
      else {
    	$('.catalog_top_menu').removeClass('catalog_top_menu_scroll');
      }
    }
  	
 	$(window).bind('scroll', scrollWatcher);
	
	/* CONSULTANT */
	$('#online_consultants .bage .expand_btn').live('click',function(){
        $(this).parent().toggleClass('bage_collapsed');
    });
	
	/* LIST OF PACKAGES */
	$('body').click(function(event){
		$('.products_list .item .list_of_packages.opened .heading').click();
	});
	
	$('.product_info .prod_image_big a.no_piro').click(function(event){
		event.preventDefault();
		var rev = $(this).attr('rev');
		$('.product_info .photo_teasers .pirobox_gall').filter(function(index){
			return $(this).attr('rev') == rev;
		}).trigger('click');
	});
	
	$('.product_info .photo_teasers_no_piro .prod_image_small').click(function(event){
		event.preventDefault();
		var img = $(this).children('img')[0];
		var filename = $(img).attr('src').substring($(img).attr('src').lastIndexOf('/')+1);
		img = new Image();
		img.onload = function(){
			$('.product_info .prod_image_big').html(img);
		}
		img.src = '/sites/default/files/imagecache/product_node/products/'+filename;
	});
	
	$('.product_info .list_of_packages input').click(function(){
		var price = $(this).parents().find('.product_info').children().find('.price');
		var package_id = $(this).attr('value');
		var new_price = $('.pkg_'+package_id+'_price').html();
		$(price).html(new_price);
		var img_rel;
		if(img_rel = $(this).attr('rel')){
			var filename = $('#'+img_rel).attr('src').replace(/^.*\//, '');
			var img = new Image();
			img.src = '/sites/default/files/imagecache/product_node/products/'+filename;
			if($('.product_info .prod_image_big a').length>0){
				var rev = $('#'+img_rel).parent().attr('rev');
				$('.product_info .prod_image_big a').attr('href','/sites/default/files/imagecache/product/products/'+filename).empty().html(img);
				$('.product_info .prod_image_big a').attr('rev',rev);
			}else{
				$('.product_info .prod_image_big').html(img);
			}
		}
	});
	
	$('.products_list .list_of_packages input').click(function(){
		var img_rel,
			image_fid,
			element;

		element = $(this).parents('.item');
		
		if(img_rel = $(this).attr('rel')){
			image_fid = img_rel.substring(img_rel.lastIndexOf('_')+1)
			$.ajax({
			  url: "/image_fid/"+image_fid,
			  success: function(data){
				if(data){
					$(element).find('.picture a').html($(data));
				}
			  }
			});			
		}
	});
	
	$('.products_list .item .list_of_packages input').click(function(){
		var price = $(this).parents('.item').children('.price');
		var package_id = $(this).attr('value');
		var new_price = $('.pkg_'+package_id+'_price').html();
		$(price).html(new_price);
	});

	$('.product_info .list_of_packages input:radio:checked').click();
	$('.products_list .item .list_of_packages input:radio:checked').click();
	
	$('.products_list .item .see_packages').click(function(event){
		event.stopPropagation();
		$('.products_list .item .list_of_packages.opened .heading').click();
		//$(this).addClass('invisible');
		$(this).parents('.item').find('.list_of_packages').removeClass('hidden').addClass('opened');
	});

	$('.products_list .item .list_of_packages').click(function(event){
		event.stopPropagation();
	});
	
	$('.products_list .item .list_of_packages .heading').click(function(event){
		event.stopPropagation();
		$(this).parent().attr('style','').addClass('hidden').removeClass('opened');
		//$(this).parents('.item').children('.see_packages').removeClass('invisible');
	});
	/*-------*/
	
	$('.equal_heights').equalHeights();
	
	$('.messages .close_message').click(function(){
		$(this).parent().fadeOut(function(){
			$(this).remove();
		});
	});
	
	$('#special_offer .button').click(function(){
		if($('#special_offer').hasClass('hide_offer')){
			$('#special_offer .products_list').slideToggle(400);
			$('#special_offer').toggleClass('hide_offer');
			$('#special_offer .equal_heights').equalHeights();
		}else{
			$('#special_offer .products_list').slideToggle(400,function(){
				$('#special_offer').toggleClass('hide_offer');
			});			
		}		
	});	
	
	$('.catalog_menu .item, .catalog_menu_mini .item').hover(function(){
		if($(this).children().find('.pic_hover').length>0){
			$(this).children().find('.pic').hide();
			$(this).children().find('.pic_hover').show();
		}},function(){
			$(this).children().find('.pic_hover').hide();
			$(this).children().find('.pic').show();
		}
	);

	$('.catalog_menu .nested_items .link, .catalog_menu_mini .nested_items .link').mouseenter(function(){
		var index = $(this).parent().children().index(this);	
		$(this).parents('.item').find('.pictures').children('.pic').hide(0,function(){
			$(this).parents('.item').find('.pictures').children('.pic_nested').eq(index).show();
		});
	});

	$('.catalog_menu .nested_items .link, .catalog_menu_mini .nested_items .link').mouseout(function(){
		$(this).parents('.item').find('.pictures').children('.pic_nested').hide(0,function(){
			$(this).parents('.item').find('.pictures').children('.pic').show();
		});
	});
	
	$('.catalog_menu .item').click(function(event){
		if(!event.target.href){
			location.href = $($(this).find('.link')[0]).attr('href');
		}
	});
	
	$('.products_list .item').click(function(event){
		if(!event.target.href && event.target.nodeName!="INPUT"){
			location.href = $($(this).find('.title')[0]).children('a').attr('href');
		}
	});
	
	$('a.show_popup').mouseover(function(event){
		var rel = $(this).attr("rel");
		var left = $(this).offset().left-160;
		var top = $(this).offset().top;
		$('.spec_popup.'+rel).css('top',top);
		$('.spec_popup.'+rel).css('left',left);
		$('.spec_popup.'+rel).fadeIn();
	});
	$('a.show_popup').mouseout(function(event){
		var rel = $(this).attr("rel");
		$('.spec_popup.'+rel).fadeOut();
	});
	
});

function loadBg() {
	$('#page_bg_left').css('background', 'url(/sites/all/themes/frontend/images/page_left_bg.png) left top no-repeat');
	$('#page_bg_right').css('background', 'url(/sites/all/themes/frontend/images/page_right_bg.png) right top no-repeat');
}

function windowOnLoad(){
	loadBg();
}

window.onload = windowOnLoad;


