$(document).ready(function(){
	var BebeDorm = {
		
		footerH : 55,
		
		init : function(){
			
			BebeDorm.zebra();
			
			BebeDorm.calculateBottom();
						
			// show submenu
			$submenu = $('header .primary li nav');
			$submenu.each(function(){
				$(this).parent('li').mouseover(function(){
					$submenu.show();
				})
				
				//$(this).parent('li').mouseout(function(){
				//	$submenu.hide();
				//})
				$(this).mouseout(function(){
					$(this).hide();
				});
			});
			
			// main carousel
			$('#showcase ul').cycle({ 
			    fx: 'fade' ,
			    pager : '#bubbles',
			    before : BebeDorm.carouselLegends
			});
						
			//$('#bubbles a').html('');
			
			
			// popups - instructions
			$('.instructions span').click(function(){
				// determine the type and position of the instruction step
				var cssClass  = $(this).attr('class');
				cssClass = cssClass.split(' ');
				cssClass = cssClass[0].split('_');
				var type = cssClass[0];
				var position = cssClass[1];
				
				// call the popup
				BebeDorm.showPopUpSlide(type,position);
			});
			
			// popups - videos
			$('.videos div').click(function(){
				// determine the type and position of the instruction step
				var cssClass  = $(this).attr('class');
				cssClass = cssClass.split(' ');
				cssClass = cssClass[0].split('_');
				var type = cssClass[0];
				var position = cssClass[1];
				
				// call the popup
				BebeDorm.showVideo(type,position);
			});
			
			
			// popups  CAM enlarged products
			$('.thumbCam').click(function(){
				var product = $(this).attr('title');
				
				// position the popup
				BebeDorm.positionPopUp('enlarge_'+product);
				
				//show the popup
				$('#enlarge_'+product).show();
				
				// load the appropriate enlarged image (save bandwidth)
				$('.enlargedImage').addClass('enlarged_'+product);
			});
			
			// display available colors
			$('.availableColors span').mouseover(function(){
				var selectedColor = $(this).attr('name'); // determine the selected color

				$('.enlargedImage').removeClass('pos1 pos2 pos3 pos4 pos5 pos6 pos7 pos8 pos9 pos10 pos11');// remove all the possible colors form the enlarged picture
				$('.enlargedImage').addClass('pos'+selectedColor);// apply the selected color
			});
			
			
			// popups  BEBE DORM enlarged products
			$('.thumbBebeDorm').click(function(){
				var product = $(this).attr('title');
				var title = $(this).children('legend').text();
			
				// position the popup
				BebeDorm.positionPopUp('enlarge_bebedorm');
				
				//show the popup
				$('#enlarge_bebedorm').show();
				$('#enlarge_bebedorm h1').text(title);
				
				// load the appropriate enlarged image (save bandwidth)
				$('.enlargedImage').addClass('enlarged_'+product);
			});
			
			
			// Boba Carrier Features
			$('.featuresBobaCarrier #footstraps').show();
			$('.featuresBobaCarrier nav span').click(function(){
				var title = $(this).attr('title');
				$('.featuresBobaCarrier nav li').attr('class','direct');
				$(this).parent().attr('class','selected');
				$('.featuresBobaCarrier article').hide();
				$('.featuresBobaCarrier article#'+title).show();
			});
			
			// popups  BOBA CARRIER enlarged products
			$('.thumbBoba').click(function(){
				var product = $(this).attr('title');
		
				// position the popup
				BebeDorm.positionPopUp('enlarge_bobacarrier');
				
				//show the popup
				$('#enlarge_bobacarrier').show();
				
				// load the appropriate enlarged image (save bandwidth)
				$('.enlargedImage').addClass('enlarged_'+product);
			});
			
			
			// treat form result
			
			$('.formresult').delay(5000).slideUp(500);
			
			
			
		},
		
		carouselLegends : function(){
			$('#carouselnotes #legend ').html(this.title);
		},
		
		calculateBottom : function(){
			var canvasH;
			
			if ($('#content').height() < $(window).height()) $('#homeSpacer').css('height', ( $(window).height() -  $('#content').height() -55 )+'px');		
			//$('#wrapper').css({'height':canvasH+'px'});
		},
		
		positionPopUp : function(id){
			$('#mask').show();
			var top = 0;
			var left = 0;
			var $myPopup = $('#'+id);
			left = ($(window).width() - $myPopup.width() )/2;
			top =  ($(window).height() - $myPopup.height() )/2;
			$myPopup.css({top:top+'px',left:left+'px'});
			
			$('.closePopup').click(function(){
				$(this).parent().fadeOut(200,function(){
					$('#mask').hide(); // hide the mask
					$('#'+id+' ul').cycle('destroy'); //destroy any initiated carousel
					$('.bubbles').children().remove(); // remove all the navigation bubbles
					$('#instructions_video').children('iframe').remove();// remove any iframe for videos
//					$('#enlarge_bebedorm .enlargedImage, #enlarge_bobacarrier .enlargedImage').attr('class','enlargedImage');
					$('.enlargedImage').attr('class','enlargedImage');

					$('#enlarge_bebedorm h1').text('');

					
				});				
			});
		},
		
		showPopUpSlide : function (type, position){
				
			var pager = '#bubblesInstructions_'+type;
			var id = 'instructions_'+type;
			$('#'+id).show();
				
			// position the popup
			BebeDorm.positionPopUp(id);
			
			// create the carousel
			$('#'+id+' ul').cycle({ 
			    fx: 'fade',
			    timeout: 5000,
			    startingSlide : position - 1,
			    pager : pager
			});
		},
		
		showVideo : function (type, position){
			var id = 'instructions_video';
			$('#'+id).show();
			
			// position the popup
			BebeDorm.positionPopUp(id);
			
			if (type == 'vimeo') var iframe = '<iframe src="http://player.vimeo.com/video/'+position+'?title=0&amp;byline=0&amp;portrait=0" width="600" height="337" frameborder="0"></iframe>';
			if (type == 'youtube') var iframe = '<iframe width="600" height="337" src="http://www.youtube.com/embed/'+position+'" frameborder="0" allowfullscreen></iframe>';
			$('#'+id).append(iframe);

		},
		
		zebra : function(){
			$('.listing>div:odd').addClass('odd');
			$('.listing>div:even').addClass('even');
		}
		

	}
	
	BebeDorm.init();






});
