var stID,_stmtBundleItems = 1;

$(document).ready(function(){

    // Statements
    $('.js #commentform').hide();

    if($('.comments').length) {
        var statements = $('.statement');
        var _stmtsCount = statements.length;
        statements.slice(_stmtBundleItems).hide();
    }

    $('.more_comments a').click(function(){
        $('.statement[style="display: none; "]').slice(0, _stmtBundleItems).slideDown();
        return false;
    });


$(window).bind('resize', function() {document.location=window.location;});

	
    if ($(window).width() >= 1100){
		
			$(".l_cartbox").stickySidebar({
            timer: 400
          , easing: "easeOutExpo"
    });
		}
	
    $('.slides:not("#homeslides, .firstslide")').slides({
        generateNextPrev: false,
        play: 0,
        effect: 'fade'
    });

    $('#homeslides').slides({
        generateNextPrev: false,
        play: 3000,
        pagination: false
    });
	

    $('.contact input:text, .contact textarea').each(function(){
        switch(this.tagName) {
            case 'INPUT':
                if($(this).val() == 'Name' || $(this).val() == 'E-Mail') {
                    jQuery.data(this,'value', $(this).val());
                }
            break;
            case 'TEXTAREA':
                if($(this).text() == 'Ihre Nachricht') {
                    jQuery.data(this,'value', $(this).text());
                }
            break;
        }
    });

    $('.contact input:text., .contact textarea').focus(function() {
        switch(this.tagName) {
            case 'INPUT':
                if($(this).val() == jQuery.data(this,'value')) {
                    $(this).val('');
                }
            break;
            case 'TEXTAREA':
                if($(this).text() == jQuery.data(this,'value')) {
                    $(this).text('');
                }
            break;
        }
    });

    $('.contact input:text., .contact textarea').blur(function() {
        switch(this.tagName) {
            case 'INPUT':
                if($(this).val() == '') {
                    $(this).val(jQuery.data(this,'value'));
                }
            break;
            case 'TEXTAREA':
                if($(this).val() == '') {
                    $(this).text(jQuery.data(this,'value'));
                }
            break;
        }
    });

    if($('p.error').length) {
        stID = $('p.error').first().parent().parent().parent().attr('id');
        if(!stID) {
            stID = 'scrollto';
            $('p.error').first().parent().parent().parent().attr('id', stID);
        }
        var t = setTimeout ( "$.scrollTo( $('#' + stID) , 300, {axis : 'y'});", 1000 );
    }
    if($('p.confirm').length) {
        stID = $('p.confirm').first().parent().attr('id');
        if(!stID) {
            stID = 'scrollto';
            $('p.confirm').first().parent().attr('id', stID);
        }
        var t = setTimeout ( "$.scrollTo( $('#' + stID) , 300, {axis : 'y'});", 1000 );
    }
    
		$('#agbText, #widerrufText').hide();
		$('a#agb').click(function(){
		if(!$(this).is('.open')){
			$(this).removeClass('open').addClass('open');	
		$('#agbText').slideDown();
		return false;
		}else{
			$(this).removeClass('open');	
			$('#agbText').hide();	
			}
		})
		
		$('a#widerruf').click(function(){
		if(!$(this).is('.open')){
			$(this).removeClass('open').addClass('open');	
		$('#widerrufText').slideDown();
		return false;
		}else{
			$(this).removeClass('open');	
			$('#widerrufText').hide();	
			}
		})
   
		
		
    
    var twitter =  $('.twitter,.twitter_count').length;
    var facebook = $('.fblike,.fblikebox').length;

    pageUrl = 'http://ilmgold.com';

    $('.twitter').replaceWith('<a style="display: none;" href="http://twitter.com/share" class="twitter-share-button" data-text="Tolles Porzellan von Ilmgold!" data-count="none" data-lang="de">Tweet</a>');
    $('.twitter_count').replaceWith('<a style="display: none;" href="http://twitter.com/share" class="twitter-share-button" data-text="Tolles Porzellan von Ilmgold" data-count="horizontal" data-lang="de">Tweet</a>');
    $('.fblike').replaceWith('<fb:like href="' + pageUrl + '" layout="button_count" show_faces="false" width="450" font=""></fb:like>');
    //$('.fblikebox').replaceWith('<fb:like-box href="http://www.facebook.com/pages/Ilmgold/155455861181402" width="213" height="250" colorscheme="dark" show_faces="false" stream="false" header="false"></fb:like-box>');

    if(twitter) {
        var tw = document.createElement('script'); tw.type = 'text/javascript'; tw.async = true;
        tw.src = 'http://platform.twitter.com/widgets.js';
        var tws = document.getElementsByTagName('script')[0]; tws.parentNode.insertBefore(tw, tws);
    }
    if(facebook ) {
        var fb = document.createElement('script'); fb.type = 'text/javascript'; fb.async = true;
        fb.src = 'http://connect.facebook.net/de_DE/all.js#xfbml=1';
        var fbs = document.getElementsByTagName('script')[0]; fbs.parentNode.insertBefore(fb, fbs);
    }
		
		
//Kollektion Switcher	

$('.singleproducts').hide();
$('.collection').find('p.switcher').each(function(){
var url = '#'+$(this).parent().attr('id');
$(this).prepend('<a class="nogrid active" href="'+url+'"><span class="hide">Kollektion in der Übersicht</span></a><a class="grid" href="'+url+'"><span class="hide">Produkte dieser Kollektion</span></a>')	
})

$switchView('.grid, .nogrid');


//Guest Order / AdressSwitcher

$('.shipping_form').hide();

if ($('.shipping_form').is('.open')){
	$('.shipping_form').show(); 	
	$('.shipping_form_access').hide();
	
	}
if ($('.guest_checkout').is('.opened')){
	  setTimeout ("$.scrollTo( '.billing_method', 1200)", 1000)
}	
	

$('.shipping_form_access').append('<p> <a  class="button_arrow_r">Lieferanschrift eingeben</a></p>');


$('.shipping_form_access a').click(function(){
	$('.shipping_form_access').hide();
	$('.shipping_form').show().prepend('<a class="close"><span class="hide">Schlie&szlig;en</span></a>');
	
	$('.shipping_form a.close').click(function(){
		$('.shipping_form_access').show();
		$('.shipping_form').hide();
  })
	
})



})

$switchView = function(linkClass){
  $(linkClass).each(function(){
   $(this).click(function(){
     var ancestor = $(this).parent().parent();
     if ($(this).hasClass('grid')){
      ancestor.find('.collectionimage').hide()
      ancestor.find('.singleproducts').fadeIn(200, function(){
      $.scrollTo( '#'+ancestor.attr('id'), 400);
      })
     }else{
      ancestor.find('.collectionimage').fadeIn(200, function(){
      $.scrollTo( '#'+ancestor.attr('id'), 400);
      })
      ancestor.find('.singleproducts').hide()
     }
     $(this).parent().find('a').removeClass('active');
     $(this).addClass('active');
     return false;
   })
  })
}



