function initiatehomepage() {
	var imageobjects = $('.image:not(.hidden)'),
		multiplier = .1,
		sizer = 1+multiplier,
		reseter = 1-multiplier;
	
	imageobjects.draggable().hover(function () {
			var offset = $(this).offset();
			$(this).css({
				'left' : (offset.left-((this.naturalWidth*sizer)-$(this).width())/2)+'px',
				'top' : (offset.top-((this.naturalHeight*sizer)-$(this).height())/2)+'px',
				'width' : ($(this).width()*sizer)+'px',
				'height' : ($(this).height()*sizer)+'px'
		    });
		},function () {
			var offset = $(this).offset();	
			$(this).css({
				'left' : (offset.left-(this.naturalWidth-$(this).width())/2)+'px',
				'top' : (offset.top-(this.naturalHeight-$(this).height())/2)+'px',
				'width' : this.naturalWidth+'px',
				'height' : this.naturalHeight+'px'
		    });
		}).not('#logo').each(function(index){
		$(this).delay(index*1000).fadeIn(2500);
		
		if(imageobjects.not('#logo, #nickelback_container').length==(index+1)){
			t = setTimeout("initiatenickelback()",((index+1)*1000));
		}
	});
	
}

function initiatehomepagerotator() {
	var rotatorobjects = $('.container'),
		switcher = $('.switcher_item'),
		preventaction = $('.preventaction'),
		rotationobject;

	$(switcher).hover(function () {
	    $(this).css({
	    	'color' : '#fff',
	    	'opacity' : '.75',
	    	'filter' : 'alpha(opacity=75)'	
	    });
	  }, function () {
	    $(this).css({
	    	'background' : '#333',
	    	'color' : '#fff',
	    	'opacity' : '.25',
	    	'filter' : 'alpha(opacity=25)'	
	    });
	 }).each(function(index){
		$(this).click(function(){
			preventaction.show();
			if(index>0){
				rotatorobjects.filter('.active').removeClass('active').animate({
				    opacity: 'toggle',
				    left: '-'+rotatorobjects.width()+'px'
				  }, 1000,function() {
				  	
				  	if($(this).next('.container').length){
						rotationobject = $(this).next();
				  	}else{
						rotationobject = rotatorobjects.eq(0);
				  	}
				  	
				  	rotationobject.addClass('active').css('left',rotatorobjects.filter('.active').width()+'px').animate({
						opacity: 'toggle',
						left: '0px'
					}, 1000,function(){
						preventaction.hide();
					});
				});		
 			}else{
 				rotatorobjects.filter('.active').removeClass('active').animate({
				    opacity: 'toggle',
				    left: rotatorobjects.width()+'px'
				  }, 1000,function() {
				  	
				  	if($(this).prev('.container').length){
						rotationobject = $(this).prev();
				  	}else{
						rotationobject = rotatorobjects.last();
				  	}
				  	
				  	rotationobject.addClass('active').css('left','-'+rotatorobjects.filter('.active').width()+'px').animate({
						opacity: 'toggle',
						left: '0px'
					}, 1000,function(){
						preventaction.hide();
					});
				  	
				});		
			}
		});
	});
	
}

function initiatenickelback() {
	var nickbackobject =  $('#nickelback_container'),
		preventdefault = false;
	
	nickbackobject.draggable().fadeIn(2500,function(){
		
		$('.options:first', nickbackobject).click(function(){
			
			if(preventdefault==false){
				preventdefault = true;
				$('ul:first ul:first .item', nickbackobject).each(function(index){
					
					$(this).delay(index*50).show(0,function(){
						if(index){
							$('.item', nickbackobject).hide();
						}
						if($('ul:first ul:first .item', nickbackobject).length==(index+1)){
							
							preventdefault = false;
							
							$('.options', nickbackobject).show().each(function(index){
								
								$(this).click(function(){
									
									if(preventdefault==false){
										
										preventdefault = true;
										
										if(index==0){
											$('ul ul .item', nickbackobject).hide().first().show();
											preventdefault = false;
											
										}else{
										
											$('ul ul:eq('+(index)+') .item', nickbackobject).each(function(index2){
												
												$(this).delay(index2*50).show(0,function(){
													
													$('.item', nickbackobject).hide();
													
													if($('ul ul:eq('+(index)+') .item', nickbackobject).length==(index2+1)){
														preventdefault = false;
													}										
													
												});
											});
											
										}
									}
									
								});
								
							});					
						}
						
					});
				
				});
			}
			
		});
	});
	
	$('ul ul', nickbackobject).each(function(){
		$('.item:last', this).click(function(){
			$('ul ul .item', nickbackobject).hide().first().show();
		});	
	});
	
}

$(document).ready(function(){
	var homepageimages =  $('img.image');
	
	initiatehomepagerotator();
		
	homepageimages.last().load(function() {
		initiatehomepage();
		
	
	});
});


