//NoSelect plugin, keeps the user from selecting an element
jQuery.fn.noSelect=function(){var a='none';return this.bind('selectstart dragstart mousedown',function(){return false}).css({MozUserSelect:a,WebkitUserSelect:a,userSelect:a})};

$(function() {


    if (jQuery('.faq_list').length > 0) {
        jQuery('.faq_list .answer').hide();
        jQuery('.faq_list h3').css('cursor', 'pointer').click(function(){
            //if the section is already opened, do nothing
            if(jQuery('#'+jQuery(this).attr('for')).is(':visible')){
                return
            }
            jQuery('.faq_list .answer').slideUp(300,function(){
            });
            jQuery('#'+jQuery(this).attr('for')).slideDown();
        });

    }
    $('.orbit_carousel').noSelect();


    //Hide the content if we're in the single product page, so we can fade it in after it's loaded
    if (jQuery('.orbit_carousel').length) {
        jQuery('#container .field').css('opacity', 0);
    }

    $("#home .orbit_carousel").jcarousel({
        visible: 1,
        scroll: 1,
        wrap: 'circular',
        animation: 300
    });

    $(".produkte .orbit_carousel").jcarousel({
        visible: 1,
        scroll: 1,
        wrap: 'circular',
        animation: 300,
        itemFirstInCallback:{
            onBeforeAnimation: function(car, el){
                id = jQuery(el).attr('data-id');
                //Sort of fade out the container before the ajax call
                jQuery('#product_info').animate({'opacity': '0.1'}, 50);
                url = (jQuery('html').attr('lang') == 'fr')?'producte-ajax':'product-ajax';
                console.log(url);
                jQuery.get(url+'/items/'+id, function(data){
                    jQuery('#product_info').html(data);
                    jQuery('#product_info').animate({'opacity': '1'}, 100);
                });
            }
        }
    });


    //In IE7 we cannot use ::after, so I find this cleaner
    $('.site_block').before('<div class="site_block_pre"></div>');
    $('.site_block').after('<div class="site_block_post"></div>');

    //In IE7 we cannot use ::after, so I add this to the titles only for IE7
    if (jQuery('body').hasClass('ie7')) {
        jQuery('.block h1').append('<div class="after_h1">__</div>');
    }

    //Handle keys
    $(document).keydown(function(e){
        if (e.keyCode == 39) { 
            jQuery('.jcarousel-next').trigger('click');
            return false;
        }
        if (e.keyCode == 37) { 
            jQuery('.jcarousel-prev').trigger('click');
            return false;
        }
    });

    //Swap error and field position
    jQuery('.ce_form p.error').each(function(){
        var el = $(this);
        el.insertAfter(el.next())
    });
           
    if (jQuery('#home').length > 0) {
        jQuery('#main_menu a').click(function(e){
            e.preventDefault(); 
            jQuery('#right .site_block').first().css('position', 'relative').animate({left:'-2000px'});
            jQuery('#right .site_block_post').first().css('position', 'relative').animate({left:'-2000px'});
            jQuery('#right .site_block_pre').first().css('position', 'relative').animate({left:'-2000px'});
            jQuery('#main .site_block').first().css('position', 'relative').animate({left:'-2000px'});
            jQuery('#main .site_block_post').first().css('position', 'relative').animate({left:'-2000px'});
            jQuery('#main .site_block_pre').first().css('position', 'relative').animate({left:'-2000px'});
            jQuery('#main .site_block').last().css('position', 'relative').animate({left:'2000px'});
            jQuery('#main .site_block_post').last().css('position', 'relative').animate({left:'2000px'});
            jQuery('#main .site_block_pre').last().css('position', 'relative').animate({left:'2000px'});
            link = jQuery(this).attr('href');
            setTimeout(function(){
                document.location = link;
            },500);
        });
    }

});
    
//Youtube player, when clicking on a thumbnail, load the video in the large player
var ytplayer;
$(document).ready(function() {
    $(document).find('.ytmini').each(function(i) {
    	var vidid = '';
    	$(this).find('.ytvidid').each(function() {
    		vidid = $(this).html();
    	});
    	
    	$(this).click(function() {
    		ytplayer.loadVideoById(vidid);
    	});
    	
    	if (i == 0){
    		var params = { allowScriptAccess: "always" };
    	    var atts = { id: "myytplayer" };
    	    swfobject.embedSWF("http://www.youtube.com/v/"+vidid+"?enablejsapi=1&playerapiid=ytplayer", "ytapiplayer", "540", "330", "8", null, null, params, atts);
    	}
    });
});
$(window).load(function(){
    //Png fix.
    if ($().pngFix) {
        $(document).pngFix();
    }   
});
function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
}

