var $j = jQuery.noConflict();
$j(document).ready(function() {	

	
    $j(".back").backgroundScale({
	    imageSelector: ".bgImg"
    });
    
    $j('.messages').css({opacity:'0', top:'-20px'});

    $j('.messages').animate({
        opacity: 1,
        top: '0px'
        }, 500, 'easeInOutExpo', function() {
            $j('.messages').delay(3000).animate({
             opacity: 0,
             top: '-20px'
             }, 1000, 'easeInOutExpo');
    });

	// Scrollbar
	if($j("#scrollbar1").size() != 0){
		$j('#scrollbar1').tinyscrollbar({sizethumb: 32});        	
	};
	
	// Selectboxes
	$j(".selectBox").selectbox();
	$j(".product-custom-option").selectbox();
	$j(".super-attribute-select").selectbox();
	
	//$j('.roskildeSlides').cjFlashySlideShow({preset: "cubism"});
	//
	
	
	//fade of bgImg
        $j(".bgImg").fadeIn(3000);

	// animate submenu
	$j(".mainNav ul li").hover(function(){
		$j(this).addClass('open');
		$j(this).find('.children').stop().animate({
			paddingTop:'15px',
			display:'inline'
		},150);
	}, function(){
		$j(this).removeClass('open');
		$j(this).find('.children').stop().animate({
			paddingTop:'-=15px',
			display:'none'
		});
	});
	
/*
	$j(".mainNav ul li").mouseover(function(){
			$j(this).find('.children').stop().animate({
				paddingTop:'25px',
				display:'inline'
			},200);
	});
	
	$j(".mainNav ul").find('li').mouseleave(function(){
		$j(this).find('.children').stop().animate({
			paddingTop:'-=25px',
			display:'inline'
		},200);
		
		alert('sdfsdf');
	});
*/	
        // Search
	$j('.searchBtn').click(function() {
		if($j('.searchBox').hasClass('close')){
			$j('.searchBox').addClass('open');
			$j('.searchBox').animate({
				right: '0px'
			}, 250, 'easeInOutCirc');
			$j('.searchBox').removeClass('close');
	       }
		else{
			$j('.searchBox').addClass('close');
			$j('.searchBox').animate({
				right: '-210px'
			}, 800, 'easeOutCirc');
			$j('.searchBox').removeClass('open');
	       }
	});
	
	
	
	
        // Search
	$j('.showHideItemsInBasket').click(function() {
		if($j('.basketItems').hasClass('close')){
			$j('.basketItems').addClass('open');
			$j('.basketItems').slideUp('fast');
			$j('.basketItems').removeClass('close');
	       }
		else{
			$j('.basketItems').addClass('close');
			$j('.basketItems').slideDown('fast');
			$j('.basketItems').removeClass('open');
	       }
	});	
	
	
	
	// Fix IE problem med selectboxe
	$j('.sbHolder').click(function() {
		if($j(this).find('a:first-child').hasClass('sbToggleOpen')){
			$j('.sbHolder').css('zIndex','5000');
			$j(this).css('zIndex','100000');
	       }
		else{
			$j(this).css('zIndex','5000');
	       }
	});
     
	// Search - clear input
        $j('.searchBox form input[type=text]').focus(function(){showHideInputValue($j(this))}).blur(function(){showHideInputValue($j(this))});
	$j('.searchBlock form input#name').focus(function(){showHideInputValue($j(this))}).blur(function(){showHideInputValue($j(this))});
	$j('.searchBlock form input#description').focus(function(){showHideInputValue($j(this))}).blur(function(){showHideInputValue($j(this))});
	$j('.searchBlock form input#produktnr').focus(function(){showHideInputValue($j(this))}).blur(function(){showHideInputValue($j(this))});
	$j('.searchBlock form input#price').focus(function(){showHideInputValue($j(this))}).blur(function(){showHideInputValue($j(this))});
	$j('.searchBlock form input#price_to').focus(function(){showHideInputValue($j(this))}).blur(function(){showHideInputValue($j(this))});

	$j('.loginBlock form input[type=text]').focus(function(){showHideInputValue($j(this))}).blur(function(){showHideInputValue($j(this))});
	$j('.loginBlock form input[type=password]').focus(function(){showHideInputValue($j(this))}).blur(function(){showHideInputValue($j(this))});
	
	
  // originale slider
  var currentPosition = 0;
  var slideWidth = 560;
  var slides = $j('.slide');
  var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $j('#slidesContainer').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
  $j('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert left and right arrow controls in the DOM
  $j('#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
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  $j('.control')
    .bind('click', function(){
    // Determine new position
      currentPosition = ($j(this).attr('id')=='rightControl')
    ? currentPosition+1 : currentPosition-1;

      // Hide / show controls
      manageControls(currentPosition);
      // Move slideInner using margin-left
      $j('#slideInner').animate({
        'marginLeft' : slideWidth*(-currentPosition)
      }, 800, 'easeInOutExpo');
    });

  // manageControls: Hides and shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
    if(position==0){ $j('#leftControl').hide() }
    else{ $j('#leftControl').show() }
    // Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $j('#rightControl').hide() }
    else{ $j('#rightControl').show() }
    }

	
});





// Functions
function showHideInputValue(argObj) {
    if ($j(argObj).val() == "")
        $j(argObj).val($j(argObj).prev().html())
    else if ($j(argObj).val() == $j(argObj).prev().html())
        $j(argObj).val('')
    
}
