// Enforce compatibility with ProtoType JS
jQuery.noConflict();

var slideUp;

jQuery(document).ready(
    function() {
		// Old browser text
		if (jQuery.browser.msie && jQuery.browser.version < 8) {
			jQuery('body').prepend('<div class="noscript"><div class="noscript-inner"><p><strong>You are using an old version of Internet Explorer / Je gebruikt een oude versie van Internet Explorer.</strong></p><p>For the best experience <a href="http://windows.microsoft.com/nl-NL/internet-explorer/products/ie/home" target="_blank" style="text-decoration:underline">please upgrade to the latest version</a>.</p></div></div>');
		}

		// Next/back tabs hover
		jQuery('#next-tab').hover(
			function()
			{
				jQuery('a img', this).attr('src', '/skin/frontend/doortje/doortje/images/next_mouse-over.png');
			},
			function()
			{
				jQuery('a img', this).attr('src', '/skin/frontend/doortje/doortje/images/next.png');				
			}
		);
		jQuery('#back-tab').hover(
			function()
			{
				jQuery('a img', this).attr('src', '/skin/frontend/doortje/doortje/images/back_mouse-over.png');
			},
			function()
			{
				jQuery('a img', this).attr('src', '/skin/frontend/doortje/doortje/images/back.png');				
			}
		);
		
		// Button hover
		jQuery('.doortjebutton, button.button, button.span, .discount-form button, .add-to-cart input[type=button]').hover(
			function()
			{
				jQuery(this).css('color', '#fff');
			},
			function()
			{
				jQuery(this).css('color', '#000');
			}
		);
		
		jQuery('.my-account-link').not('.selected').hover(
			function()
			{
				jQuery(this).css('background', '#FF5858');
			},
			function()
			{
				jQuery(this).css('background', '#000');
			}
		);
		
		// Currency hover
		jQuery('.mini-currency span:not(:first)').hover(
			function()
			{
				jQuery(this).css('background-color', '#FF5858');
				jQuery(this).css('color', '#000');
			},
			function()
			{
				if (!jQuery(this).hasClass('selected')) {
					jQuery(this).css('background-color', '#000');
					jQuery(this).css('color', '#fff');
				}
			}
		);
		
        // Top-level main-menu
        jQuery.each(
            jQuery('.tops > ul > li.level0'), 
            function(k, v){ 
                jQuery(v).css('width', jQuery(v).width() + 'px');
            }
        );	
        
		jQuery('.selection-tool-title').hover(
			function()
			{
				if(jQuery('#narrow-by-list:visible').length > 0) {
					jQuery('img', this).attr('src', '/skin/frontend/doortje/doortje/images/selectionToolOver.png');
				} else {
					jQuery('img', this).attr('src', '/skin/frontend/doortje/doortje/images/selectionToolClosedOver.png');
				}
			},
			function()
			{
				if (jQuery('#narrow-by-list:visible').length > 0) {
					jQuery('img', this).attr('src', '/skin/frontend/doortje/doortje/images/selectionTool.png');
				} else {
					jQuery('img', this).attr('src', '/skin/frontend/doortje/doortje/images/selectionToolClosed.png');
				}
			}
		);
		
		jQuery('.selection-tool-title').toggle(
			function()
			{
				jQuery('#narrow-by-list').slideUp('fast', function(){ jQuery('.selection-tool-title img').attr('src', '/skin/frontend/doortje/doortje/images/selectionToolClosedOver.png'); });
			},
			function()
			{
				jQuery('#narrow-by-list').slideDown('fast', function(){ jQuery('.selection-tool-title img').attr('src', '/skin/frontend/doortje/doortje/images/selectionToolOver.png'); });
			}
		);

        // Menu hovers
        jQuery('.tops li.level0').not('.no-hover').click(
            function()
            {
				if (jQuery('a:first', this).css('background-image') != 'none' && (!jQuery('a:first', this).hasClass('selected') || jQuery('#products-bar-content:visible').length > 0)) {
					jQuery('a:first', this).css('background-image', 'none');
					slideBarUp();
					jQuery('.level0 a.selected').css('background-image', 'url(/skin/frontend/doortje/doortje/images/tab.png)');
					jQuery('.level0 a.selected').css('background-repeat', 'no-repeat');
				} else {
					jQuery('.tops li.level0').each(
						function()
						{
							jQuery('a:first', this).css('background', 'none');
						}
					);
					jQuery('a:first', this).css('background-image', 'url(/skin/frontend/doortje/doortje/images/tab.png)');
					slideBarDown(this);
				}
				return false;
            }
        );
		
		jQuery('.products-bar .title-bar').click(
			function() {
				if (jQuery('.top-right .products-bar .products-bar-content:visible').length == 0) {
					//slideBarDown(this);
				} else {
					jQuery('.tops li.level0').each(
						function()
						{
							jQuery('a:first', this).css('background', 'none');
						}
					);
					slideBarUp();
				}
			}
		);
        
        // Set tbe values for the top login field
        jQuery('#mini-login').focus(
            function(){
                jQuery(this).attr('value', '');
                jQuery(this).css('color', '#000000');
                jQuery(this).css('font-style', 'normal');
            }
        );

		jQuery('#mini-login').blur(
			function(){
				if (jQuery(this).val() == '') {
					jQuery(this).attr('value', '- e-mail -');
					jQuery(this).css('font-style', 'italic');
				}
			}
		);	
			
        jQuery('#mini-password').focus(
            function(){                
                jQuery(this).replaceWith('<input type="password" name="login[password]" id="mini-password" />');
                jQuery('#mini-password').css('color', '#000000');
                jQuery('#mini-password').css('font-style', 'normal');
                jQuery('#mini-password').focus();
            }
        );
        
        jQuery('.add-to-cart input[type=button], .discount-form button, button.button, button.span').hover(
            function(){
                jQuery(this).css('background-color', '#00D8D5');                
            },
            function(){
                jQuery(this).css('background-color', '#848484');                
            }
        );
        
        jQuery('.clickable').hover(
            function(){
                jQuery(this).addClass('clickable-hover');
            },
            function()
            {
                jQuery(this).removeClass('clickable-hover');
            }
        );
        
        jQuery('.back-to-overview-header-text').hover(
            function(){
                jQuery(this).css('color', '#58C8AB');
                jQuery(this).parent().css('background-image', 'url("/skin/frontend/doortje/doortje/images/backtoOverview-hover.png")');
            },
            function(){
                jQuery(this).css('color', '#000');
                jQuery(this).parent().css('background-image', 'url("/skin/frontend/doortje/doortje/images/backtoOverview.png")');
            }
        );
        
        jQuery('.tops .home').hover(
            function()
            {
                jQuery('img', this).attr('src', '/skin/frontend/doortje/doortje/images/homeButtonOver.png');
                jQuery(this).css('color', '#000');
            },
            function()
            {
                jQuery('img', this).attr('src', '/skin/frontend/doortje/doortje/images/homeButton.png');
                jQuery(this).css('color', '#9D9C9D');
            }
        );
        
        jQuery('.tops .home').click(
            function()
            {
                window.location = '/';
            }
        );
        
        if (jQuery('.more-views > ul').length > 0) {            
            jQuery('.more-views img[src=' + jQuery('.product-image a').attr('href') + ']').parent().parent().remove();
            jQuery('.more-views > ul').jcarousel(
                {
                    scroll: 1,
                    buttonNextHTML: null,
                    buttonPrevHTML: null,
                    initCallback: carouselInitCallback,
                    buttonPrevCallback : carouselButtonPrevCallback,
                    buttonNextCallback : carouselButtonNextCallback,
                    start: 2/*,
                    wrap: 'circular',*/
                }
            );
        }
        
        jQuery('.add-to-wishlist-text').hover(
            function()
            {
                jQuery(this).css('border-bottom', '1px solid black');
            },
            function()
            {
                jQuery(this).css('border-bottom', 'none');
            }
        );
        
        function carouselButtonPrevCallback (instance, control, visible)
        {
            if (visible) {
                jQuery('#carousel-prev').fadeIn();
            } else {
                jQuery('#carousel-prev').fadeOut();
            }
        }
        
        function carouselButtonNextCallback (instance, control, visible)
        {
            if (visible) {
                jQuery('#carousel-next').fadeIn();
            } else {
                jQuery('#carousel-next').fadeOut();
            }
        }

		jQuery('a.shipping-costs-cart').click(			
			function() {				
				jQuery('#shipping-costs').dialog(
		            {
		                dialogClass: 'infodialog',
		                width: 525,
		                height: 481,
		                resizable: false,
						autoOpen: true,
		                title: jQuery('#shipping-costs-title').text()
		            }
		        );
				return false;
			}
		);
        
        jQuery('p.product-image a').hover(zoomStart, zoomStop);
        jQuery('p.product-image a').mousemove(function(e) { zoomSetBoxPosition(e, this); });
        
        jQuery('#attribute128').change(
            function()
            { 
                jQuery('#qty').find('option').remove();
                for (x = 1; x <= optionsQty.slice(jQuery(this).attr('selectedIndex'), jQuery(this).attr('selectedIndex')+1).pop(); x++) {
                    jQuery('#qty').append('<option value='+ x + '>' + x + '</option>');
                }
            }
        );
        
        jQuery('.how-to-measure').click(
            function(){
				rel =  jQuery('span:first', this).attr('rel').split(',');
				html = '';
				
				for (i = 0; i < rel.length; i++) {
					html += '<img src="/skin/frontend/doortje/doortje/images/howtomeasure/' + rel[i] + '.jpg" style="float:left" />';
				}
                jQuery('body').append('<div id="how-to-measure-dialog">' + html + '</div>');
                jQuery('#how-to-measure-dialog').dialog(
                    {
                        dialogClass: 'infodialog',
                        width: 550 * rel.length,
                        height: 480,
                        resizable: false,
                        title: jQuery('span:first', this).text(),
                        close: function (e, ui) { jQuery(this).remove(); }
                    }
                );
            }
        );
        jQuery('#email_link').hover(function(){ jQuery(this).css('color', '#c13931') },function(){ jQuery(this).css('color', '#000') });
        jQuery('#email_link').click(
            function()
            {
                jQuery('#email_form').dialog(
                     {
                        dialogClass: 'infodialog',
                        width: 425,
                        height: 481,
                        resizable: false,
                        title: jQuery(this).text(),
                        open: function(){ jQuery('input[name=bogus]').focus(); }
                    }
                );
            }
        );
        
        jQuery('#email_form_submit').click(
            function()
            {
				if (
					jQuery('#email_form input[name=email]').val().length < 5
					||
					jQuery('#email_form textarea[name=question]').val().length == 0
				) {
					return false;
				}
				
                jQuery.post(
                    window.location.href,
                    {
                        'email' : jQuery('#email_form input[name=email]').val(),
                        'product' : jQuery('#email_form input[name=product]').val(),
                        'url' : jQuery('#email_form input[name=url]').val(),
                        'question' : jQuery('#email_form textarea[name=question]').val(),
						'cc' : jQuery('#email_form input[name=cc]').attr('checked')
                    }
                );
				jQuery('#email_confirmation').dialog({
					dialogClass: 'infodialog',
					close: function(){ jQuery('#email_form').dialog('close'); },
					resizable: false
				});
            }
        );
        
        jQuery('.wishlist-footer .wishlist-titlebar').click(
            function()
            {
                if (jQuery('.wishlist-footer .wishlist-content:visible').length > 0) {
                    jQuery('.wishlist-footer .wishlist-content').slideUp();
                } else {
                    jQuery('.wishlist-footer .wishlist-content').slideDown();
                }
            }
        );
        
        jQuery('.block-layered-nav dd ol li').each(
            function()
            {
                if (jQuery('ol', this).length > 0) {
                    jQuery('a:first', this).addClass('amshopby-attr-selected');
                }
            }
        );
        
        jQuery('.header-controls .left .mini-currency span').not(':first').not('.selected').hover(
            function()
            {
                jQuery(this).css('background-color', '#FF5858');
                jQuery(this).css('color', '#000000');
            },
            function()
            {
                jQuery(this).css('background-color', '#000000');
                jQuery(this).css('color', '#FFFFFF');
            }
        );
        
        // Remove the wishlist on un-supported devices
        if (jQuery('.wishlist-footer').length > 0) {
            if (navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
                jQuery('.wishlist-footer').remove();
            };
        }
		
		jQuery('.cms-contact input, .cms-contact textarea').each(function() {
			jQuery(this).one('focus', function(){
				jQuery(this).val('');
	      	});
	    });
    }
 );

function slideBarUp() {
	jQuery('.top-right .products-bar .products-bar-content').slideUp();
	jQuery('.top-right .products-bar .title-bar').animate(
		{
			'margin-top': '0',
			'opacity': '0'
		}, 
		300,
		function() { jQuery('.top-right .products-bar .title-bar').css('display', 'none'); }
	);
}

function slideBarDown(obj) {
	// Clear previous time-out
	clearTimeout(slideUp);	

	// Copy the hover's content to the bar's content
	jQuery('.top-right .products-bar .products-bar-content').html('<ul id="' + jQuery('ul', obj).attr('id') + '">' + jQuery('ul', obj).html() + '</ul>');
	
	// If the bar is invisible, slide it down
	if (jQuery('.top-right .products-bar .products-bar-content:visible').length == 0) {
		jQuery('.top-right .products-bar .products-bar-content').slideDown();
		jQuery('.top-right .products-bar .title-bar').css('display', 'block');
		jQuery('.top-right .products-bar .title-bar').animate(
			{
				'margin-top': '312px',
				'opacity': '1'
			},
			450
		);
	}
}
 
function zoomStart() {
     if (jQuery(this).attr('rel').length == 0) {
         jQuery(this).css('cursor', 'default');
         return false;
     }
     jQuery(this).append('<div class="zoom-region-selector"></div>');     
     jQuery('body').append('<div class="zoom-region-display"></div>');
     
     zoomregionPosition = jQuery(this).position();
     jQuery('.zoom-region-display').css('left', (zoomregionPosition.left + (jQuery(this).width() + 8)) + 'px');
     jQuery('.zoom-region-display').css('top', (zoomregionPosition.top) + 'px');
     jQuery('.zoom-region-display').css('background-image', 'url(\'' + jQuery(this).attr('rel') + '\')');
 }
 
 function zoomStop() {
     jQuery('.zoom-region-selector').remove();
     jQuery('.zoom-region-display').remove();
 }
 
 function zoomSetBoxPosition(e, zoomregion)
 {
     zoomregionPosition = jQuery(zoomregion).position();
     
     zoomregionWidth = jQuery(zoomregion).width();
     zoomregionHeight = jQuery(zoomregion).height();
     
     selectorWidth = jQuery('.zoom-region-selector').width();
     selectorHeight = jQuery('.zoom-region-selector').height();
     
     regionLeft = e.pageX - zoomregionPosition.left - (selectorWidth/2);
     regionTop = e.pageY - zoomregionPosition.top - (selectorHeight/2);
     
     if (regionLeft < 0) {
         regionLeft = 0;
     }
     if (regionLeft > zoomregionWidth - selectorWidth) {
         regionLeft = zoomregionWidth - selectorWidth;
     }
     
     if (regionTop < 0) {
         regionTop = 0;
     }
     if (regionTop > zoomregionHeight - selectorHeight) {
         regionTop = zoomregionHeight - selectorHeight;
     }
     
     jQuery('.zoom-region-selector').css('left', regionLeft + 'px');
     jQuery('.zoom-region-selector').css('top', regionTop + 'px');
     
     zoomLeft = regionLeft * 2.5 * -1;
     zoomTop = regionTop * 2.5 * -1;
     
     
     // Correct negative position
     if (zoomLeft > 0) {
         zoomLeft = 0;
     }
     if (zoomTop > 0) {
         zoomTop = 0;
     }
     
     jQuery('.zoom-region-display').css(
        'background-position', 
        zoomLeft + 'px ' + zoomTop + 'px'
     );
     
     jQuery('.zoom-region-selector').show();
     jQuery('.zoom-region-display').show();
 }
 
function carouselInitCallback(carousel) {
    jQuery('#carousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#carousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
}
 
function swapCarouselImage(clicked)
{
    // Get values
    newHref = jQuery('img:first', clicked).attr('src');
    newSrc  = jQuery(clicked).attr('href');
    newRel  = jQuery(clicked).attr('rel');
    oldHref = jQuery('.product-image a:first').attr('href');
    oldSrc  = jQuery('.product-image img:first').attr('src');
    oldRel  = jQuery('.product-image a:first').attr('rel');
    
    // Write values
    jQuery('.product-image a:first').attr('href', newHref);
    jQuery('.product-image img:first').attr('src', newSrc);
    jQuery('.product-image a:first').attr('rel', newRel);
    
    // Swap the old to the clicked --Deprecated--
    //jQuery('img:first', clicked).attr('src', oldHref);
    //jQuery(clicked).attr('href', oldSrc);
    //jQuery(clicked).attr('rel', oldRel);
    
    // Check if the zoom-icon should be hidden
    if (newRel.length == 0) {
        jQuery('.zoom-icon').hide();
    } else {
        jQuery('.zoom-icon').show();
    }
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}
