function showPopup(url, initialTitle, post)
{
    if (!url)
    {
        alert('No url given for the popup');
    }

    if (initialTitle)
    {
        $('#popup-dialog').dialog('option', 'title', initialTitle);
    }

    if (!post) post = '';

    $('#popup-dialog').html('<p class="center"><img src="/images/v2/loading.gif" alt="loading" /></p>').dialog('open');
    $('#popup-dialog').load(url, post, function()
    {
        var title = $('h4:first', this).hide().html();

        if (title == undefined)
        {
            title = $('h1:first', this).hide().html();
        }

        $(this).dialog('option', 'title', title);
        $(this).dialog('option', 'position', 'center');
        $(':input:visible:not(.date):not(.time):eq(0)', this).focus();
        //updateButtons();
    });
}

//var selectBankMessage = 'Please choose a bank to continue';
//var extraCashMessage = 'Please bear in mind that paying cash will cost you extra. ' +
 //   'Do you want to continue?';

$(function()
{
	if (document.getElementById('sharethis') != null)
	{
		var script = document.createElement('script');
		script.src = "http://w.sharethis.com/button/sharethis.js#publisher=921dd87f-33f2-41b3-99d2-627b282c5bc1&amp;type=website&amp;post_services=facebook%2Ctwitter%2Cemail%2Cgbuzz%2Clinkedin%2Cblogger%2Cfriendfeed%2Cwordpress%2Cnewsvine&amp;linkfg=%23333333&amp;button=false&amp;onmouseover=false";
		document.getElementsByTagName('head')[0].appendChild(script);
		
		script.onload = function()
		{
			var shared_object = SHARETHIS.addEntry({
				title: document.title,
				url: document.location.href
			});
		
			shared_object.attachButton(document.getElementById('sharethis'));
		};
	}
	
	// $('.product .image a#zoom-anchor').jqzoom({
	// 		zoomWidth: 283,
	// 		zoomHeight: 298,
	// 		title: false
	// 	});
	
	$('.front-image').css({'cursor': 'pointer'}).click(function()
	{
		window.location = $(this).attr('rel');
	});
	
    $('a.external').attr('target', '_blank');
    
    $.fn.idle = function(time)
    {
        var o = $(this);

        o.queue(function()
        {
            setTimeout(function()
            {
                o.dequeue();
            }, time);
        });

        return o;
    };

    $('p.success.fade').idle(4000).hide('slow');

    //$('ppppppppppppppppppppppppppppppppppppppppp')
    $('.ask-question').click(function(ev)
    {
        ev.preventDefault();

        showPopup($(this).attr('href'), 'Ask question');

        return false;
    });

    $('#popup-dialog').removeClass('hide').dialog({
        autoOpen: false,
        bgiframe: true,
        modal: true,
        width: 550,
        resizable: false,
        buttons: {
            Close: function()
            {
                $(this).dialog('close');
            }
        },
        open: function ()
        {
            //disableKeyCodeNavigation();
        },
        close: function()
        {
            //enableKeyCodeNavigation();
        }
    });

    var initial = $('#updated-default').val();
    var email = $('#updated-mail');
    if (email.val() == '') email.val(initial);

    email.focus(function()
    {
        if ($(this).val() == initial)
        {
            $(this).val('');
        }
    });

    email.blur(function()
    {
        if ($(this).val() == '')
        {
            $(this).val(initial);
        }
    });

    //console.log(email.parents('form'));
    $(email.parents('form')[0]).submit(function()
    {
        if (email.val() == initial)
        {
            email.val('');
        }

        showPopup($(this).attr('action'), '', {
            updated_email: email.val()
        });

        return false;
    });

    $('#currency-form #currency').change(function()
    {
        $('#currency-form').submit();
    });

    $('#language select#currency, #shopping-cart select').selectbox();

    $('#order input[name=update_amounts]').hide();
    $('#order select[name^=order]').change(function()
    {
        $('#order input[name=update_amounts]').click();
    });

    //$('#order a.photo').lightbox();

    $('.image .little-photos img').css({cursor: 'pointer'}).click(function()
    {
        $('.image #main-image').attr('src', this.src); // at least show some nizzle
        $('.image #main-image').attr('src', this.src.replace('little', 'huge'));
    });

    
    slideShow(4000);

    $('a.confirm').click(function()
    {
        return confirm('Are you sure you want to continue?');
    });

    // $('#order #shopping-cart .options a img').hover(function()
    // {
    //     $(this).attr('src', this.src.replace('trash', 'trash-open'));
    // }, function()
    // {
    //     $(this).attr('src', this.src.replace('trash-open', 'trash'));
    // });

    $('li div.overlay').css({cursor: 'pointer'}).click(function()
    {
        window.location = $('a', this).attr('href');
        return false;
    });

    /*$('ul.products li div.overlay').css({top: '119px'}).parent().hover(function()
    {
        $('div.overlay', this).stop().animate({top: '90px'}, {queue: false, duration: 200});
    }, function()
    {
        $('div.overlay', this).stop().animate({top: '119px'}, {queue: false, duration: 200});
    });*/

    if (1==2 && !(jQuery.browser.msie && jQuery.browser.version == '6.0'))
    {
        $('img.hover-enlarge').each(function()
        {
            //var parent = $(this).parent();

            $(this).parent().wrapInner('<div />');

            var parent = $(this).parent().css({
                position: 'relative'
            });

            //$(this).clone().appendTo(parent);

            $(this).clone().appendTo(parent).attr(
                'src', $(this).attr('src').replace('little', 'middle')).css({
                position: 'absolute',
                top: 0,
                left: 0
            }).hover(function()
            {
                $(this).stop().css({
                    zIndex: 1000
                }).animate({
                    left: -18,
                    top: -18,
                    width: 100,
                    height: 100
                }, 500);
            }, function()
            {
                $(this).stop().css({
                    zIndex: 'auto'
                }).animate({
                    top: 0,
                    left: 0,
                    width: 55,
                    height: 55
                }, 500);
            });
        });
    }

    if ($('#coupon-toggle').length)
    {
        $('#coupon-holder').hide();
        $('#coupon-toggle').html($('#coupon-toggle').html().replace(':', '?')).css({
            cursor: 'pointer', textDecoration: 'underline'
        }).click(function()
        {
            if ($('#coupon-holder').is(':visible'))
            {
                $('#coupon-holder').hide();
                $(this).html($(this).html().replace(':', '?')).css({
                    textDecoration: 'underline'
                });
            }
            else
            {
                $('#coupon-holder').show();
                $(this).html($(this).html().replace('?', ':')).css({
                    textDecoration: 'none'
                });
            }
        });
    }

    $('#ideal-bank').focus(function()
    {
        $('#payment-ideal').attr('checked', 'checked');
    });

    $('#payment-form').submit(function()
    {
        if ($('#payment-ideal').is(':checked'))
        {
            if ($('#ideal-bank').val() == '')
            {
                alert(selectBankMessage);
                $('#ideal-bank').focus();
                return false;
            }

            //$(this).attr('action', 'https://payment.buckaroo.nl/gateway/ideal_payment.asp');
        }
        else if ($('#payment-creditcard').is(':checked'))
        {
            //$(this).attr('action', 'https://payment.buckaroo.nl/sslplus/request_for_authorization.asp');
        }
        else if ($('#payment-cash').is(':checked'))
        {
            return confirm(extraCashMessage);
        }
        else if ($('#payment-bank').is(':checked'))
        {

        }
        else if ($('#payment-paypal').is(':checked'))
        {

        }
        else
        {
            alert('Please choose your way of paying');
            return false;
        }

        return true;
    });
});

function slideShow(speed)
{
    if (!speed) speed = 4000;

    // hide all
    $('ul.slideshow li').css({opacity: 0.0});

    // show first
    $('ul.slideshow li:first').css({opacity: 1.0}).addClass('show');

    if ($('ul.slideshow li').lenght == 1)
    {
        return;
    }

    // set timer
    var timer = setInterval('gallery()', speed);

    // pause on hover
    $('ul.slideshow').hover(function ()
    {
        clearInterval(timer);
    }, function ()
    {
        timer = setInterval('gallery()', speed);
    });
}

function gallery()
{
    // disable animation
    //return;

    //if no IMGs have the show class, grab the first image
    var current = $('ul.slideshow li.show');

    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    var next = current.next().length ? current.next() : $('ul.slideshow li:first');

    // hide current
    current.animate({opacity: 0.0}, 1000).removeClass('show');

    // show next
    next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
}
