/**
 * website.js
 */

var website = {
	
	init: function() {

		website.init_thumbnail_rollovers();
		website.init_selected_nav_logos();
		
		$('#portal_images li:last').addClass('top');
		website.init_portal_image_cycles();
		
		$('#menu_logo').hover(function() {
			$('#menu_logo ul .item_logo').hide().fadeIn('normal');
		}, function() {
			$('#menu_logo ul .item_logo').hide();
		});
		
		/** deprecated 
		$('ul.item1').hover(function() {
			$(this).find('li').eq(1).hide().fadeIn('normal');
		}, function() {
			$(this).find('li').eq(1).hide();
		});
		**/

		$('#header-tabs > ul > li').hoverIntent(function() {
			$(this).find('ul').fadeIn('fast');
		}, function() {
			$(this).find('ul').hide();
		});

		$('#header_content ul.ullinks li').hoverIntent(function() {
			$(this).find('ul').hide().fadeIn('fast');
		}, function() {
			$(this).find('ul').hide();
		});
		
		if( $.browser.msie ) {
			if( $.browser.version == '7.0' ) {
				website.sortout_ie7();
			}
		}
		
	}
	
	,sortout_ie7: function() {
		
		var browser_height = $(window).height();
		var document_height = $(document).height();

		var header_height = $('#header').height();
		var content_height = $('.centralise_content').height();
		var footer_height = $('#total-footer').height();
		
		var do_height = document_height - footer_height;

		$('.footer, #new_links, .footer_little, #footer_bg, #footer_illus, #flash, .ruth, .rainbowbit').css({
			position: 'fixed'
		});
		
		$('.centralise_content').css({
			'padding-bottom': (footer_height + 100) + 'px'
		});

		$('.centralise_content_readybed').css({
			'padding-bottom': (footer_height + 200) + 'px'
		});

		
	}
	
	,init_portal_image_cycles: function() {
		
		$('#portal_images li.top').fadeIn('slow', function() {
			$('#portal_images li').show();
			
			if( $('#portal_images li').length > 1 ) {
				setTimeout(function() {
					$('#portal_images li.top').fadeOut('slow', function() {
						$(this).removeClass('top').prependTo( $(this).parent() );
						$('#portal_images li:last').addClass('top');
						website.init_portal_image_cycles();
					});
				}, 5000);
			}
		});
		
	}
	
	,init_thumbnail_rollovers: function() {
		
		$('#thumbnail_rollovers ul').addClass('jcarousel-skin-wa');

		$('#thumbnail_rollovers ul').jcarousel({
			wrap: 'circular'
			,scroll: 3
		});

		$('.centralise_content').append('<div class="thumbnail_mainimage"></div>');
		
		if( !$('.centralise_content').hasClass('centralise_content_portal') ) {
			$('.centralise_content').css({
				 'background-position': 'right top'
				,'background-image': 'none'
			});
		}
		
		$('#thumbnail_rollovers a').each(function() {
			
			var href = $(this).attr('href');
			
			if( href.search(/.html/i) === -1 ) {
				$(this).css('cursor', 'default');
			}
			
		});
		
		$('#thumbnail_rollovers a').mouseover(function() {
			
			var img = $(this).attr('rel');
			var href = $(this).attr('href');

			if( href.search(/.html/i) === -1 ) {
				img = href;
			}

			if(img) {
				if( $('.thumbnail_mainimage img').attr('src') != img ) {
					$('.thumbnail_mainimage').stop().fadeOut('fast', function() {
						$(this).removeClass('ontop');
						if( href != img ) {
							$(this).html('').addClass('ontop').html('<a href="'+href+'"><span><img src="' + img + '" alt="" /></span></a>');
						} else {
							$(this).html('').addClass('ontop').html('<img src="' + img + '" alt="" />');
						}
						$(this).fadeTo('slow', 1);
					});
				}
			}
			
		});
		
		$('#thumbnail_rollovers a').click(function() {
			
			$(this).blur();
			
			var href = $(this).attr('href');
			
			if( href.search(/.html/i) > 0 ) {
				return true;
			} else {
				return false;
			}
			
		});
		
		if( $('#thumbnail_rollovers li').length <= 4 ) {
			
			$('.jcarousel-prev, .jcarousel-next').click(function() {
				return false;
			})
			.unbind('click.jcarousel')
			.css('cursor', 'default')
			.hide();
			
			$('#thumbnail_rollovers').css({
				 float: 'right'
				,'margin-right': 0
				,'right': 0
			});
		}
		
		var x=0;
		var imgs = [];
		$('#thumbnail_rollovers a').each(function() {
			var img = $(this).attr('rel');
			var href = $(this).attr('href');

			if( href.search(/.html/i) === -1 ) {
				img = href;
			}
			
			imgs[x] = new Image();
			imgs[x].src = img;
			
			x++;
		});
		
		$('#thumbnail_rollovers a:first').trigger('mouseover');
		
		window.onload = function() {
			
		};

	}
	
	,init_selected_nav_logos: function() {
		
		var url = String(window.location);
		
	
		
		// play section
		if( url.search(/play\/go-glow/i) > 0 ) {
			$('.top_links_goglow').addClass('top_links_goglow_selected');
		}
		if( url.search(/play\/dream-town/i) > 0 ) {
			$('.top_links_dreamtown').addClass('top_links_dreamtown_selected');
		}
		if( url.search(/play\/pop-n-fun/i) > 0 ) {
			$('.top_links_popnfun').addClass('top_links_popnfun_selected');
		}
		if( url.search(/play\/nursery/i) > 0 ) {
			$('.top_links_nursery').addClass('top_links_nursery_selected');
		}
		if( url.search(/play\/rock-n-rolla/i) > 0 ) {
			$('.top_links_rocknrolla').addClass('top_links_rocknrolla_selected');
		}		
		// dreamtown section
		if( url.search(/ready-bed\/my-first/i) > 0 ) {
			$('.top_links_myfirst').addClass('top_links_myfirst_selected');
		}

		if( url.search(/ready-bed\/junior/i) > 0 ) {
			$('.top_links_junior').addClass('top_links_junior_selected');
		}

		if( url.search(/ready-bed\/tween/i) > 0 ) {
			$('.top_links_tween').addClass('top_links_tween_selected');
		}
	
	
		// winnie section
		if( url.search(/winnie-the-pooh-interactive-friend/i) > 0 ) {
			$('.interactiveFriend').addClass('interactiveFriend_selected');
		}

		if( url.search(/winnie-interactive-friend-setup-instructions/i) > 0 ) {
			$('.setupInstructions').addClass('setupInstructions_selected');
		}			
	
	}
	
};


// sort flash out on all browsers (to allow display below stuff on ie)
 
function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                try {
                    if (children[j] != null) {
                        var theName = children[j].getAttribute('name');
                        if (theName != null && theName.match(/flashvars/i)) {
                            new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                        }
                    }
                }
                catch (err) {
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}




$(document).ready(website.init);


$(document).ready(function() {
	 
	 //start fix_flash so flash displays below things on ie when you want it too
	fix_flash(); 	 
	 
	 
	 //Retailers links dropdown DROPPED 31st AUGUST 2011
	 
	 // hide for dudes with javaasctipt
	 //$('#retailersUL').hide();
	 
	 //slide toggle
	 //$('.whereToBuy').click(function() {
		//$('#retailersUL').slideToggle('slow', function() {
		//});
	 //});
	 
	
	 
	 
	 
	 // POOH PLAYER
	 
	 
	 //$('#morning').hide();	
	 //$('#playtime').hide();	
	
	//var videoCodeMorning = $('#morning').html();
	//var videoCodePlaytime = $('#playtime').html();
	//var videoCodeVid = $('#vid').html();
	//var videoCodeVid1 = $('#vid1').html();
	//var videoCodeVid2 = $('#vid2').html();
	
	//videoCodeVidTwo = $('#two').html();
	
	
	
	//$('#morning').hide();
	//$('#playtime').remove();
	
	//$('#winnieButton').click(function() {
	  //$('#winnieButton').animate({
		//right: '280',
	  //}, 2000, function() {
		// Animation complete.
	  //});
	//});
	 
	 
	//WINNIE THE POOH PAGE 
	 
	//hide the vids
	$('#VIDone').hide();
	$('#VIDtwo').hide();
	$('#VIDthree').hide();
 	$('#VIDfour').hide();
 	$('#VIDfive').hide();
 	$('#VIDsix').hide();




  //set movement increments
    var moveRight = 265;
    var moveRight = moveRight + 'px';	 
	 
    
    // set clickCounter to start position
    var clickCounter = 0;
	
    
	//set dayNight background position movement amount
	$('#dayNight').css('backgroundPosition',63);
	var backgroundPos = -250;
    var backgroundPos = backgroundPos + 'px';   




    // move right
    $('.winnieButton').click(function() {
        
		
        // add 1 to clickCounter
        clickCounter +=1;   
		//alert(clickCounter); 
	
		if(clickCounter == 0) {
			//if they're restarting the cycle
			//remov last vid
			//$('#VIDfive').fadeOut(3000);
			//reset button to play button
			
			$('.winnieButton').css("background-image", "url(../assets/images/2011/button.png)");
			
			//reset background pos
			$('#dayNight').animate({backgroundPosition: 63},2000);
						
			//take backgroundPos value off of current background-position
			//$('.winnieButton').animate({right: '-=' + moveRight},2000);
			
			//send the button back to the start
			$(this).animate({right: '+=' + 265},3000);
			
			//show the speech bubble and ting
			$('#frameBG, .speechBubbles, .poohInstructionButton, .poohInstructionsText').fadeIn(2000);
			
			
			
		}
		
		if(clickCounter == 1) {
			//remove the speech bubbles and ting
			$('#frameBG, .speechBubbles, .poohInstructionButton, .poohInstructionsText').fadeOut(2000);
			$('#VIDone').fadeIn(3000);
			
		//$('#dayNight').animate({backgroundPosition: '+=' + backgroundPos},2000);
			$('.winnieButton').fadeOut(1000);
			
			$("#dayNight").delay(2000).queue(function(n) {
				$(this).animate({backgroundPosition: '+=' + backgroundPos},40000);
				
				n();
			});	
			
			//$(".winnieButton").delay(42000).css("background-image", "url(../assets/images/2011/button-end.png)");
			
		}

		if(clickCounter == 2) {
			
			
			$('#VIDone').fadeOut(3000);
			// for some homosexual reason, ie8 ignores fadeOut, so hide() instead
			$('#VIDone').hide();
			
			//shift the dayNight background
			//$('#dayNight').animate({backgroundPosition: '+=' + backgroundPos},2000);
	
			
			//change the button to return image		
			$('.winnieButton').animate({right: '300px'},2000);
			$('#dayNight').animate({backgroundPosition: 63},2000);
			
			//start clickCounter at -1 because next time it's clicked this will start it at 0, and therefor reset to the beginning
			clickCounter = -1;
		}			
		
		
	});// close move right
	
	
	
	
	$('.continue').hide();
	$('.start-again').hide();
	$('.battery').hide();
	$('.battery-on').hide();
	$('.battery-on2').hide();
	$('.screen2').hide();

	$('#from').hide();

	
		clickCounterSetup = 0;


	// setup instructions
	 $('.start').click(function() {
		 
		 
		
        // add 1 to clickCounter
        clickCounterSetup +=1;   
		//alert(clickCounterSetup); 
	
		if(clickCounterSetup == 0) {
			$('.startimg').faceIn(2000);
		}
		
		
		if(clickCounterSetup == 1) {
			$('.startimg').fadeOut(2000);
			

			$('.screen1-text').fadeOut(2000);
			$('.battery').delay(2000).fadeIn(3000);
			$('.battery-on').delay(4000).fadeIn(500);
			$('.battery-on2').delay(6000).fadeIn(500);
			
			$('.continue').delay(8000).fadeIn(1000);

		}
		
		if(clickCounterSetup == 2) {
		
			$('.start').fadeTo(2000, 0.2).delay(8000).fadeTo(2000, 1);
			$('.screen1, .screen1-text, .battery, .battery-on, .battery-on2').fadeOut(2000);
			$('.screen2').fadeIn(2000);
			
			$(".screen2-text").delay(2000).animate({"top": "-=95px"}, 1500).delay(2000).animate({"top": "-=85px"}, 1500).delay(2000).animate({"top": "-=96px"}, 1500);


			
		}
	
		if(clickCounterSetup == 3) {
		
			$('.start').fadeTo(2000, 0.2).delay(11500).fadeTo(2000, 1);

			$(".winnie-screen3-first").delay(500).animate({"top": "+=267px"}, 2500);

			$(".winnie-screen3-second").delay(2000).animate({"top": "-=267px"}, 2500);

			$(".winnie-screen3-third").delay(4000).animate({"top": "+=267px"}, 2500);
			
			$(".winnie-screen3-text").delay(6000).animate({"left": "+=415px"}, 2500).delay(4000).animate({"left": "+=375px"}, 2500);

		}
	
	
		if(clickCounterSetup == 4) {
		
			$('.screen2, .screen2-text, .winnie-screen3-first, .winnie-screen3-second, .winnie-screen3-third, .winnie-screen3-text').fadeOut(2000);
			
			$('#from').delay(2000).fadeIn(2000);
			
			$('.continue').fadeOut(2000);
			
			//$('.start-again').delay(2000).fadeIn(2000);

		}	


		if(clickCounterSetup == 5) {
			clickCounterSetup == 0;
		}
	 });
	
	
			 
			 
			 
			 
/****************************************
SLIDEY NAVIGATION			 
****************************************/
			 
	$('.slideyInner p').hide();
	
	$('.slideyInner form').hide();
	
	
	
		
		
			 
			 
	$('#slideyHome').hover(
		function(){
			//move div
			$(this).stop().animate({width:"295px"},{queue:false,duration:1000});
			//show the text and background
			$('#slideyHome .slideyInner p').delay(500).fadeTo(800,1);
			$('#slideyHome .slideyInner img.background').show().animate({top:"0"},{queue:false,duration:1000});
		},function(){
			//move div back
			$(this).stop().animate({width:"133px"},{queue:false,duration:500});
			//hide the text and background
			$('#slideyHome .slideyInner p').stop().fadeOut(100);
			$('#slideyHome .slideyInner img.background').stop().fadeOut(100).animate({top:"80px"},{queue:false,duration:500});
	});	
	
	
	
	
	
	
	
			 
	$('#slideyPlay').hover(
		function(){
			//move div
			$('#slideyPlay,#slideyHome').stop().animate({width:"295px"},{queue:false,duration:1000});
			//show the text
			$('#slideyPlay .slideyInner p').delay(500).fadeTo(800,1);
			$('#slideyPlay .slideyInner img.background').show().animate({top:"0"},{queue:false,duration:1000});
			
		},function(){
			//hide the bar
			$('#slideyPlay,#slideyHome').stop().animate({width:"133px"},{queue:false,duration:500});//was 1000
			//hide the text
			$('#slideyPlay .slideyInner p').stop().fadeOut(100); // was 400
			$('#slideyPlay .slideyInner img.background').stop().fadeOut(100).animate({top:"80px"},{queue:false,duration:500}); // was 400 and 1000
	});	
	
	



	
			 
	$('#slideyReadyBed').hover(
					
		function(){
			//move div
			$('#slideyPlay,#slideyHome,#slideyReadyBed').stop().animate({width:"295px"},{queue:false,duration:1000});
			//show the text
			$('#slideyReadyBed .slideyInner p').delay(500).fadeTo(800,1);
			$('#slideyReadyBed .slideyInner img.background').show().animate({top:"0"},{queue:false,duration:1000});
					
		},function(){
			//hide the bar
			$('#slideyPlay,#slideyHome,#slideyReadyBed').stop().animate({width:"133px"},{queue:false,duration:500});
			//hide the text
			$('#slideyReadyBed .slideyInner p').stop().fadeOut(100);
			$('#slideyReadyBed .slideyInner img.background').stop().fadeOut(100).animate({top:"80px"},{queue:false,duration:500});
	});		
	
	



	
			 
	$('#slideyCharacter').hover(
					
		function(){
			//move div
			$('#slideyPlay,#slideyHome,#slideyReadyBed,#slideyCharacter').stop().animate({width:"295px"},{queue:false,duration:1000});
			//show the text
			$('#slideyCharacter .slideyInner p').delay(500).fadeTo(800,1);
			$('#slideyCharacter .slideyInner img.background').show().animate({top:"0"},{queue:false,duration:1000});
					
		},function(){
			//hide the bar
			$('#slideyPlay,#slideyHome,#slideyReadyBed,#slideyCharacter').stop().animate({width:"133px"},{queue:false,duration:500});
			//hide the text
			$('#slideyCharacter .slideyInner p').stop().fadeOut(100);
			$('#slideyCharacter .slideyInner img.background').stop().fadeOut(100).animate({top:"80px"},{queue:false,duration:500});
	});		
	
	

	
			 
	$('#slideySearch').hover(
					
		function(){
			//move div
			$('#slideyPlay,#slideyHome,#slideyReadyBed,#slideyCharacter,#slideySearch').stop().animate({width:"295px"},{queue:false,duration:1000});
			//show the text
			$('#slideySearch .slideyInner form').delay(500).fadeTo(800,1);
					
		},function(){
			//hide the bar
			$('#slideyPlay,#slideyHome,#slideyReadyBed,#slideyCharacter,#slideySearch').stop().animate({width:"133px"},{queue:false,duration:500});
			//hide the text
			$('#slideySearch .slideyInner form').stop().fadeOut(100);
			
	});		
		
	
	
	
	$('#siteSearchForm').submit(function(e) {
		e.preventDefault();
		
		var q = $(this).find('.searchBox').val();
		if(!q) return false;
		
		var url = '/search/' + escape(q);
		window.location = url;
	});
	
	
				 



				 
}); // close doc ready
 
 
 
 
 
 
 
 
