/*** functions ***/

function showlayer(layer){ 
	$('#' + layer).toggle(); 
}

function hidelayer(layer) {
	showlayer(layer);
} 

function initMatschplay() {
		if ($('#apdiv2').length == 0){ 
			$('#page').after('<div id="apdiv2" style="display: none; position: absolute; left: 0px; top: 0px; width: 100%; height:100%; z-index: 1000;"><div id="replace2"></div></div>');
	    }

		if ($('#matschplay').length == 0){ 
			$('#main').append('<div id="matschplay"><a href="http://get.adobe.com/flashplayer/" target="_blank"><img src="/wp-content/uploads/2010/07/fallback_matchplayer.jpg" border="0"></a></div>');
	    }

		swfobject.embedSWF("/wp-content/uploads/2010/06/matschwindow.swf", "replace2", "1200", "795", "9.0.0","", {}, {menu: "false", wmode: "transparent"},  {id: "bapdiv2"});
		swfobject.embedSWF("/wp-content/uploads/2010/06/matschplay.swf", "matschplay", "550", "530", "9.0.0","", {}, {menu: "false", wmode: "transparent"}, {id:"flash"});
	
}

var gcstp;
var stageReady=false;
if (!gcstp) {
	gcstp = {};
}

if (typeof jQuery != 'undefined') {
    $.extend(gcstp, {
    	init: function(){
    		gcstp.initGalleryTeaser();
    		gcstp.addEvents();
    		gcstp.initPlugins();	
    		
    		
    		
    		$('#header .navigation li ul li').append('<span>|</span>');
    		$('#header .navigation li ul li:last-child span').remove();
    		
    		$('#sidebar .navigation li ul li:first-child, #sidebar .navigation li ul li ul li:first-child').before('<li class="subFirstChild">&nbsp;</li>');
    		$('#sidebar .navigation li ul li:last-child, #sidebar .navigation li ul li ul li:last-child').after('<li class="subLastChild">&nbsp;</li>');
    		
    		$('#s').val('Suchen...');
	
    	},
    	
    	addEvents: function(){
    		$('#changeBgCombobox').live('change', function(event){
    		
    			var bg = $(event.target).val();
    							
    			if(bg != 0) {
					$('#bg').attr('src', bg );	
					$.get("/wp-content/themes/gcstp/bg.php", { bg: bg} );
    			}
				
			});		
    		
    		$('#s').live('blur', function(event){

    			($(this).val() == '')?$(this).val('Suchen...'):$(this).val();
				
			});	

    		$('#s').live('focus', function(event){
    			
    			($(this).val() == 'Suchen...')?$(this).val(''):$(this).val();
				
			});	
    		
		},
		
		initGalleryTeaser: function(){
			var currentPosition = 0;
			var slideWidth = 560;
			var slides = $('#galleryTeaser #slideshow div.ngg-gallery-thumbnail-box');
			var numberOfSlides = slides.length;

			// Remove scrollbar in JS
			$('#galleryTeaser #slideshow .ngg-galleryoverview').css('overflow', 'hidden');
			
			// Wrap all .slides with #slideInner div
			slides
				.wrapAll('<div id="slideInner"></div>')
				// Float left to display horizontally, readjust .slides width
				.css({
					'float' : 'left',
					'width' : slideWidth
				});
			
			// Set #slideInner width equal to total width of all slides
			$('#galleryTeaser #slideInner').css('width', slideWidth * numberOfSlides);
			
			// Insert left and right arrow controls in the DOM
			$('#galleryTeaser #slideshow')
				.prepend('<span class="control" id="leftControl">Move left</span>')
				.append('<span class="control" id="rightControl">Move right</span>');
			
			// Hide left arrow control on first load
			gcstp.manageControls(currentPosition, numberOfSlides);
			
			// Create event listeners for .controls clicks
			$('#galleryTeaser .control')
				.bind('click', function(){
				// Determine new position
				currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
			
				// Hide / show controls
				gcstp.manageControls(currentPosition, numberOfSlides);
				// Move slideInner using margin-left
			$('#galleryTeaser #slideInner').animate({
					'marginLeft' : slideWidth*(-currentPosition)
				});
			});
		},
		
		initPlugins: function(){
			if($('#content #wrapper #main').length > 0){
				$('#content #wrapper #main').jScrollPane({showArrows:false, scrollbarWidth:22, scrollbarMargin:0, dragMinHeight:21, dragMaxHeight:21});
			}
			if($('#changeBgCombobox').length > 0){
				$('#changeBgCombobox').styledSelectBox();	
			}
		},
		
		// manageControls: Hides and shows controls depending on currentPosition
		manageControls : function(position, numberOfSlides){
	        // Hide left arrow if position is first slide
	        if(position==0){ 
	        	$('#leftControl').hide();
	        }
	        else{ 
	        	$('#leftControl').show(); 
	        }
	        // Hide right arrow if position is last slide
	        if(position==numberOfSlides-1){ 
	        	$('#rightControl').hide(); 
	        }
	        else{ 
	        	$('#rightControl').show(); 
	        }
	    }
    })
    /* page load */
    $().ready(function() {
    	gcstp.init();
    });
}

