sb_content=function(){
	this.mobile=true;
	if(ecomode==1){
		this.mobile=true;	
	}else{
		this.mobile=false;	
	}
	
	this.viewportX=$(window).width();
	this.viewportY=$(window).height();
	this.sceneScroll=0;
	this.timeout=new Array();
	
	this.rollCannonBall=function(dir, targetX){
		var obj=this;
		//alert(dir);
		if(targetX){
			stopX=targetX;
		}else{
			stopX=false;
		}
		if($('#cannonBall').is(':visible')){
			
		}else{
			$('#cannonBall').show();
		}
		obj.showPlans();
		//alert(stopX);
		if(dir=='right'){
			$("#cannonBall").animate(
				{"left": (stopX+500)+"px"}, 
				800, 
				function(){
					//$('#cannonBall').hide();
					obj.updateCannonBallGhost();
				}
			);
		}else{
			$("#cannonBall").animate(
				{"left": (stopX+500)+"px"}, 
				800, 
				function(){
					obj.updateCannonBallGhost();
				}
			);
		}
		
	}
	this.clearTimeout=function(){
		var obj=this;
		
		if(obj.timeout[0]){
			//alert('a');
			clearTimeout(obj.timeout[0]);
		}
		if(obj.timeout[1]){
			clearTimeout(obj.timeout[1]);
		}
	}
	this.hidePlan=function(){
		var obj=this;

		obj.timeout[0]=setTimeout('$(\'#plan2\').fadeOut(2000)', 500);
		obj.timeout[1]=setTimeout('$(\'#plan3\').fadeOut(2000)', 1000);


	},
	this.showPlans=function(){
		var obj=this;
		obj.clearTimeout();
		$('#plan3').fadeIn(500);
		$('#plan2').fadeIn(100);
	},
	this.scrollables=function(){
		var obj=this;
		//alert('scrollables');
		$('#promotions-scrollable').scrollable({
			next :	".next",
			prev :	".prev",
			items: ".items",
			circular: true
		});$('.sbGallery .scrollable').scrollable({
			next :	".next",
			prev :	".prev",
			items: ".items",
			circular: false
		});//.autoscroll({ autoplay: false });;
		$('.prev').click(function(){return false;});
		$('.next').click(function(){return false});
		
	},
	this.summonGhosts=function(){
		var ghostCounter=0;
		$('.ghostable').each(function(){
			var parent=$(this);
			var parent_id=parent.attr('id');
			if(parent_id!=''){
				ghost_id=parent_id+'-ghost';
				
			}else{
				parent_id='parent-'+ghostCounter;
				parent.attr('id',parent_id);
				ghost_id='ghost-'+ghostCounter;
			}
			
			parent.before('<a href="#" id="'+ghost_id+'" style="position: absolute;display: block;" class="ghost"></a>');
			
			var ghost=$('#'+ghost_id);
			ghost.attr('parent_id',parent_id);
			ghost.css({'z-index': 1000});
			ghost.css({'cursor': 'pointer'});
			ghost.css({'width': parent.css('width')});
			ghost.css({'height': parent.css('height')});
			var parUrl=$('#'+ghost.attr('parent_id')).attr('href')
			ghost.attr('href',parUrl);
			ghost.click(function(){
				//alert('#'+ghost.attr('parent_id'));
				if(parUrl&&parUrl!='#'){
					document.location=parUrl;
				}else{
					$('#'+ghost.attr('parent_id')).trigger('click');	
				}
				return false;
			});
			ghostCounter++;
		});
	},
	this.init=function(){
		var obj=this;
		//setting slides dimension
		if(obj.mobile==false){
			$('.s2').css('width','30000px');
			$('.slide').css({'min-width': obj.viewportX+'px'});
			$("#cannonBall").click(function(){
		 		obj.rollCannonBall('right',$('#slide-1').position().left);
		 		obj.moveScene('slide-1');
			}); //cannon ball click
			$('#plan2').hide();
			$('#plan3').hide();
			$('#status').hide();
			obj.showPlans();
			$('#plan2').click(function(){
				$(this).hide('slow');	
			});
			var flag=true;
			var startPos=100;
			$('#scene').scroll(function(){
				obj.sceneScroll=$('#scene').scrollLeft();
				$('#status').html(obj.sceneScroll);
							
				//jestesmy w landing zone
				//ustawiamy start obliczen
				
				$('#plan2').css({'background-repeat': 'repeat-x'});
				$('#plan3').css({'background-repeat': 'repeat-x'});
				$('#plan2').css({backgroundPosition: '-'+((obj.sceneScroll*1.2))+'px 0'});
				$('#plan3').css({backgroundPosition: '-'+((obj.sceneScroll*2.2))+'px 0'});
				
			});
			//nav menu events
			$('ul.navMenu_1 .item a').each(function(){
				var elId=$(this).attr('rel').replace('#','');
				$(this).click(function(){
					//clear all items in menu
					$('.navMenu_1 li.item').removeClass('selected');
					//set selected
					$(this).parent().addClass('selected');
					obj.jumpToMenu(elId);
					return false;
				});
			});
			//here go overlays!
			$(".sbOverlay").overlay({effect: 'apple'});
			//here come ghosts! boo!
			obj.summonGhosts();
			//and the scrollables ta-da!
			obj.scrollables();
			$('#cannonBall').before('<a id="cannonBall-ghost"></a>');
			$('#cannonBall-ghost').css({'z-index':4})
			.click(
				function(){
					$('#cannonBall').trigger('click');
				});
			$("a.fancyGallery").fancybox({
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'changeSpeed'	:	100,
				'speedIn'		:	300, 
				'speedOut'		:	100, 
				'titleShow'		:	false, 
				'overlayColor'	:	'#000000',
				'overlayOpacity':	1,
				'opacity'		:  false,
				'overlayShow'	:	true
			});
		}else{
			//mobWidth=(obj.viewportX+50)+'px';
			mobWidth='1000px';
			$('.s2 .slide').css('width', mobWidth);
			
			//$('#scene').css('overflow', 'scroll');
			$('#scene').css('width', mobWidth);
			$('.s1').css('width', mobWidth);
			$('.s3').css('width', mobWidth);
			$('.s2').css('width', mobWidth);
			$('.s2 .slide ').css('width', mobWidth);
			$('.s2 .slide .container').css('width', '950px');
			//$('.s2 .slide').css('height', 'auto');
			$('.s2 .slide .container').css('margin', '0 auto');
			/*$('.slide').css({'min-width': '960px'});*/
			$('.slide').css({'background-image': 'url(bowling_site/gfx/bowling/s2_b2.jpg)'});
			$('.fullView').css({'background-image': 'url(bowling_site/gfx/bowling/back2.jpg)'});
			$('.frontPage').css({'background-repeat': 'repeat-x'});
			//$('.prev').hide();
			//$('.next').hide();
		}
		
		
		

		
		
	},
	
	this.updateCannonBallGhost=function(){
		$('#cannonBall-ghost').css({
			'left': $('#cannonBall').position().left
		});
	},
	this.moveScene=function(itemId){
		var obj=this;
		//obj.scrollables();
		obj.clearTimeout();
		$('#scene').scrollTo( $('#'+itemId), {duration: 2000, onAfter: function(){obj.hidePlan();}}); 
	},
	this.jumpToMenu=function(itemId){
		var obj=this;
		if(obj.sceneScroll-$('#'+itemId).position().left>0){
			obj.rollCannonBall('left',$('#'+itemId).position().left);
		}else{
			obj.rollCannonBall('right',$('#'+itemId).position().left);	
		}
		$('#scene .s2 .slide').removeClass('selected');
		$('#scene .s2 #'+itemId).addClass('selected');
		obj.moveScene(itemId);
	},
	//bind to thumbs!
	$('a.scrollableSeek').click(function(){
		var id=$(this).attr('id').replace('pslide-','');
		sbc.seekOnScrollable('promotions-scrollable',id);
		return false;
	});
	this.seekOnScrollable=function(scrollableId, seek){
		var obj=this;
		//alert(scrollableId);
		$('#'+scrollableId).scrollable().seekTo(seek,100);
	}
	
	//contact form
	$("#contactForm-submit").click(function(){
		var obj=this;
		sbc.sendContactForm();
		return false;
	});
	
	this.sendContactForm=function(){
		var email=$('#contactForm-email').val();
		var phone=$('#contactForm-phone').val();
		var subject='';
		var name=$('#contactForm-name').val();
		var body=$('#contactForm-body').val();
		
		var data='&email='+email+'&phone='+phone+'&name='+name+'&body='+body+'&subject='+subject;
		$.ajax({
			type: 'POST',
			url: site_adr+'/index.php?m=contact_form&a=submit',
			data: data,
			success: function(msg){
				if(msg=='1'){
					$('#contactForm-info').html('Wiadomość została wysłana. Dziękujemy za kontakt.');
					$('#contactForm').hide('slow');
				}else{
					$('#contactForm-info').html(msg);
				}
			}
		});
	}
}

$(function() {
	sbc=new sb_content();
	sbc.init();	
});

