$(document).ready(function(){
	try{
		$('.other-news').jScrollPane({showArrows:true, scrollbarWidth:9});	
	}
	catch(e){
		
	}
	
	$('ul.collection-menu a').hover(function(){
		var randomnumber=Math.floor(Math.random()*11);
		var id = $(this).attr('rel');
		if (id != null && id.length > 0) {
			var url = '/Apps/WebObjects/Branchetti.woa/wa/viewProductImagePreview?id=' + id+ '&thumb=Y&barazkja='+randomnumber;
			//alert(url);
			
			$('#preview div').css('background-image','none');
			$('#preview div').hide();
			
			$('#preview div').css('background-image','url('+ url + ')');
			$('#preview div').fadeIn();
		}
		
	}, function(){
		
	});
	
	
	$('.states-list-content ul').css({display:'none'});
	
	
	$('.region-content a.region-link').not('.selected').bind('click',function(){
		
		
		var tBody = $(this).closest('tbody');
		var tr = $(this).closest('tr');
		
		
		var activeDiv = tBody.find('ul.active');
		if(activeDiv){
			activeDiv.removeClass('active');
			activeDiv.fadeOut();
		}
		
		var activeRegion = tBody.find('a.selected');
		if(activeRegion){
			activeRegion.removeClass('selected');
		}
		
		var activeState = tBody.find('a.selected-state');
		if(activeState){
			activeState.removeClass('selected-state');
		}
		
		$(this).addClass('selected');
		var f = function(){
			tr.find('div.states-list-content ul').addClass('active')
		};
		tr.find('div.states-list-content ul').fadeIn(500,f);
		return false;
	});
	
	$('.states-list-content ul li a').click(function(){
		var activeState = $(this).parent().parent().find('a.selected-state');
		if(activeState){
			activeState.removeClass('selected-state');
		}
		$(this).addClass('selected-state');
		
		$('div.top-position-address').hide();
		$('div.bottom-position-address').hide();
	
		var currentURL = $(this).attr('href');
		
		if($(this).hasClass('top-position')){
			$.ajax({
				success:function(fetchedHTML){
					var parentTop = $('div.top-position-address').parent().offset().top;
					var parentLeft = $('div.top-position-address').parent().offset().left;
					$('div.top-position-address').html(fetchedHTML);
					$('div.top-position-address').fadeIn();
					
					$('div.top-position-address').css({top:parentTop+40, left:parentLeft+462});
					
					
				}, 
				url: currentURL
			});
		}
		
		if($(this).hasClass('bottom-position')){
			$.ajax({
				success:function(fetchedHTML){
					var parentTop = $('div.bottom-position-address').parent().offset().top;
					var parentLeft = $('div.bottom-position-address').parent().offset().left;
					
					$('div.bottom-position-address').html(fetchedHTML);
					$('div.bottom-position-address').fadeIn();
					
					$('div.bottom-position-address').css({top:parentTop+280, left:parentLeft+462});
				}, 
				url: currentURL
			});
		}
		return false;
	});
});

$(window).resize(function(){

$('div.top-position-address').hide();
$('div.bottom-position-address').hide();
});


$(window).load(function(){
	
    	positioning();
});



function positioning(){
    	var left = parseInt($('#product-nav').css('left'));
    	left = left + $('#product-nav').width() + 5;
    	$('#gallery-controller').css('left',left);
		$('#content').css('left',left);
}
