/**
 * @author Greg
 */


var Navigation = {
    init: function(){
	   $("a[rel='lightbox']").colorbox();
    }
};


var Scroller = {
	init: function(){
					var aprev= $('<a>').addClass('button').addClass('prev').addClass('disabled');
					var anext= $('<a>').addClass('button').addClass('next').addClass('disabled');
					aprev.html('prev').attr('href','#');
					anext.html('next').attr('href','#');
					var offset = 100;		
					$('.products_list ul li ul').each(function(){
						var h = $(this).height();
						var ph = $(this).parents('ul').height();
						var cont = $('<div>').addClass('scroller-warper');
						var items = $(this);
						
					
							$(items).css('position','absolute');
							$(items).css('top','0px');
							var adiv = $('<div>').addClass('scroller-pane');
						
							$(cont).append(adiv);
							cont.append(aprev);
							cont.append(anext);
							$(this).wrap(cont);
							
							$('#leftCol').css({position:'relative'});
							var parentList = $(this).parents('li');
							$(parentList).hover(
										function(){	
											$(parentList).children('.scroller-warper').css({
												display :'block' ,
												position : 'absolute',
												left:'150px',
												top : '32px'
											});	
										},
										function(){ $(parentList).children('.scroller-warper').css('display','none');   }
								
							);
							
						$('a.next').live('click' , function(event){
								var top = parseInt( $(items).css('top') );
								var height = $(items).height();
								//min the height of the window or less 
								maxTop = height - 200 ;
								if(top < 0){
									top= top + offset;
									$(items).animate( {top :top+'px',width:'125px'},1500);
								}
								else{
									$(items).animate( {top :'0px',width:'125px'},1500);
									return false;
								}
								
								
							});
							
							$('a.prev').live('click' , function(event){
								
								var top = parseInt( $(items).css('top') );
								var max = $(items).height() - offset;
								
								if(top<0 || top ==0 )
								{
									top= top - offset;
									
									
									if( Math.abs(top) > max){
										return false;
									}else{
									
										$(items).animate( {top :top+'px'},1500);
									}
								}
								return false;
							});
			
					});
					
	}
};

$(document).ready(function(){
	
	Navigation.init();
	
	$('section#newsBlock').skitter({
		velocity:2
	});
	
	$('.rounded').each(function(){
		var round = $('<div></div>').attr('class','roundedWrapper');
		$(round).css({ height: $(this).height(),width:$(this).width() });
        $(this).wrap(round);
        $(this).parent('div.roundedWrapper').append('<span class="tl"></span>');
        $(this).parent('div.roundedWrapper').append('<span class="tr"></span>');
        $(this).parent('div.roundedWrapper').append('<span class="bl"></span>');
        $(this).parent('div.roundedWrapper').append('<span class="br"></span>');		
	});
    
  if ( $.browser.msie ) 
  {
      $('div#header ul.navigation>li:first-child').hover(function(e){$(this).addClass('hover');},function(e){$(this).removeClass('hover');});
  }

	$('div#share').VisibleShare({buttons : 'facebook,twitter,email'});
	
	Scroller.init();
	
	
	function setLabel(element){
			var wrap = $('<div class="wrapimg">');
			var des = $(element).attr('alt');
			$(element).attr('style',''); 
			$(element).wrap(wrap);
			wrap = $(element).parent('.wrapimg');
			var legend = $('<p>').html( des );
			$(wrap).append(legend)	;
			$(legend).addClass( 'imglegend' );
			$(legend).width( $(element).width() - 20 );
	}
	$('.bloc2 img').wrap( function(){
			
			setLabel(this);
	})	;	
	
	$('.bloc3 img').wrap( function(){
			
			setLabel(this);
	})	;	

	/*set left menu to the same size */
	//$(window).resize(setMenu);
	
	//$('#contentCol').resize(setMenu);	
	/*
	function setMenu(){
		if( $('#contentCol').height() > 335){
		$('.products_list').height( $('#contentCol').height() );	
	}
	}
	setMenu();
	*/
	/* newsletter menu */
	/*
	if( $('#newsletternav').lenght>0){
		$('#newsletternav ul.navigation li a ').click(function(){
		
			var parent = $(this).parent('li');
			var lst = parent.children('ul');
			if( lst.length>0 ){
				$('#newsletternav ul.navigation li ul').css({display:'none'});
				$(lst).css({display:'block'});
			
				setMenu();
				return false;
			}
			
	});
	}
	*/
	if($('#newsletternav').length>0){
		
		$('#newsletternav>ul').append('<div id="newsletternavsubmenu"/>');
		$('#newsletternav>ul>li, #newsletternav>ul>li>a').bind('click',function(e){
			e.preventDefault();
			$('a.newsletter-active').removeClass('newsletter-active');
			$(this).addClass('newsletter-active');
			li = $(this).parent("li").html();
			$("#newsletternavsubmenu").html(li);
			$("#newsletternavsubmenu").show();
			return false;
		});
	}
	
});





