// JavaScript Document

jQuery(function(){
	
jQuery("nav ul#menu-hauptnavigation > li > ul").wrap('<div class="dropdown"></div>');	
var a;
var sleep = 0;

jQuery("nav ul#menu-hauptnavigation > li").mouseenter(function(){
			 	sleep = 1;
				clearTimeout(a);
				jQuery("ul#menu-hauptnavigation li").removeClass('on');
				jQuery(this).addClass('on');
				jQuery("ul#menu-hauptnavigation li:not('.on') .dropdown").fadeOut(0);
				jQuery(this).find('.dropdown').slideDown(300);	 
				jQuery(this).find('.dropdown > ul').masonry({queue: false, duration: 0});
			}).mouseleave(function(){
				sleep = 0;
				var x=jQuery(this);
				a = setTimeout(function() {                         
        		if(sleep==0) {
					x.removeClass('on');
				    x.find('.dropdown').fadeOut(100);
        		}
    			},1000);

				
		})

//BREADCRUB BUG

bugBC();

//Accordeon
if(jQuery('.accordeon').length > 0) initAccordeon();

//SEARCH
	jQuery('.breadcrumb .icons .search').click(function(){
		showSearchfield();	
	})

//SLIDERBOX
	jQuery('#slides').cycle({ 
		fx:     'fade', 
		fit: 1,
		speed:  500, 
		timeout: 12000, 
		pause: true,
		pauseOnPagerHover: 1,
		speedIn: 1500,
		speedOut: 1000,
		before: hideArticle,
		after: showArticle
	});
	
//VIDEO
jQuery('#startVideo').click(function(){
	
	bindVideo();
})

// Produktfilter
	jQuery('.wpsc_filter').change(function(){
		updateFilter();	
	})
	
	jQuery('#resetFilter').click(function(){
		window.location ="/Pianosalon/products-page";
		return false;
	})
	
//Anfrageform
	jQuery('.anfrageU').click(function(){
		jQuery('.unterricht_anfrage').slideToggle(300);
		return false;	
	})


})
	
	
function showSearchfield(){
	jQuery('#searchform_slide').animate({
		'margin-right': '0px'	
	})
	jQuery('#searchform').fadeIn(700);
	
}
	
function hideArticle(){
	var id = this.id;
	jQuery('#'+id+' article').css('display','none');
	}	
	
function showArticle(){
	
	var id = this.id;
	jQuery('#'+id+' article').delay(5000).fadeIn(1300);
	
	return true;
	}
	
function bindVideo(){
		jQuery('body').append('<div id="bgoverlay"></div>');
		jQuery('.slideWrapper').append('<div id="videooverlay"></div>');
		var txt = '<iframe src="http://player.vimeo.com/video/32071454?title=0&amp;byline=0&amp;portrait=0&amp;color=82002e&amp;autoplay=1" width="761" height="428" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>';
   		jQuery('#videooverlay').append(txt);
		jQuery('#bgoverlay').fadeTo(600,0.3);
		jQuery('#videooverlay').fadeIn(1000);
		
		jQuery('#bgoverlay').click(function(){
			jQuery('#videooverlay').fadeOut(1500,function(){
				jQuery('#vimeoplayer').unload();
				jQuery('#videooverlay').remove();	
			});
			jQuery('#bgoverlay').delay(300).fadeOut(1500,function(){jQuery(this).remove();});
			
		})
		
	// Add VideoJS to all video tags on the page when the DOM is ready
    VideoJS.setupAllWhenReady();
	
	}
	
	
function updateFilter(){
	var a = "";
	jQuery('.wpsc_filter').each(function(){
		if(jQuery(this).val() != "0"){
			var url = jQuery(this).val();
			a += url + "+";
			
		}
		
	})
	if(a == "") {} //kein selector ausgewählt
	else {
		var block = jQuery('#maincontent article');
		block.html('<img class="ajaxlaod" src="' + templateDir + '/gfx/ajax-loader.gif" />');
		
		
		jQuery.ajax({
 		 url: templateDir + '/productload.php?param='+a,
  		 async:false,
		 success: function(data) {
			block.html(data);
			
			//REINIT THICKBOX und "mehr Details"
			TB_init();
			bugBC();
			jQuery("a.additional_description_link").click(function() {
				parent_element = jQuery(this).parent(".additional_description_container, .additional_description_span");
				jQuery('.additional_description',parent_element).slideToggle('fast');
				return false;
			});
			// Produktfilter
			jQuery('.wpsc_filter').change(function(){
				updateFilter();	
			})
			jQuery('#resetFilter').click(function(){
				window.location ="/Pianosalon/products-page";
			return false;
	})
	
		}
});
			
	}
}

function bugBC(){
		if(jQuery('#prod_breadcrumb').length > 0 && jQuery('#prod_breadcrumb').html().length > 10){
			jQuery('.breadcrumb > p').html(jQuery('#prod_breadcrumb').html());
		}
}

function initAccordeon(){
	//FIRST
	jQuery('.accordeon').find('p').each(function(){
		if(jQuery(this).html() == ""){
			jQuery(this).remove();	
		}
		
	})
	jQuery('.accordeon').find('h1:first').addClass('on')
	jQuery('#accText').html(jQuery('.accordeon').find('.a_block:first').html());
	
	
	//CLICK
	jQuery('.accordeon h1').click(function(){
	jQuery(this).parent().find('h1').removeClass('on');
	jQuery(this).addClass('on');
	jQuery(this).next('.a_block').addClass('on');
	jQuery('#accText').html(jQuery(this).next('.a_block').html());
})
	
}
	
	
