$(document).ready(function()
{
	$('.login').click(function()
	{
		if ($('#modalBg').is(':hidden'))
		{
			$('#modalBg').height($(document).height());
			$('#modalBg').css('opacity', 0).show();
			$('#modalBg').fadeTo('fast', 0.5, function()
			{
				$('#loginArea').centerInClient().fadeIn('fast');
			});
		}
		return false;
	});
	
	$('span.termsShow a').click(function()
	{
		var _offset = $(this).offset();
	
		$('#termsPopup').css('top',parseInt(_offset.top)-520).css('left',parseInt(_offset.left)-400).fadeIn('fast');
		return false;
	});

	$('#termsClose').click(function()
	{
		$('#termsPopup').fadeOut('fast');
		return false;
	});
	
	$('#checkout_account_link').click(function(){$('#checkout_account_details').slideToggle('fast');return false;});
	$('#checkout_card_link').click(function(){$('#checkout_card_details').slideToggle('fast');return false;});

	$('#loginArea form').submit(function()
	{
		var _url = this.action;
		$.post(this.action, 
			   {username: $('#loginUsername').val(), password: $('#loginPassword').val(), ajax: 1, login_account: 1},
			   function(_response)
			   {
			   		$('#loginusernameError').html('');
			   		$('#loginpasswordError').html('');
				   $.each(_response,function(_key,_item) 
				   {
					   	if (_key == 'ok')
						{
							window.location.reload();
							return false;
						}
						else
						{
				   			$('#login'+_key+'Error').html('<span class="error">'+_item+'</span>');
						}
            	   });
			   },
			   'json');
		return false;
	});
	
	$('#modal_close').click(function()
	{
		$('#modalBg').fadeOut('fast',function()
		{
			$('#loginArea').fadeOut('fast');
		});
		return false;
	});
	
	$('div.imgOptions img').click(function()
	{
		var _mainImage = $('#proImageArea img:first');
		var _mainSrc = _mainImage.attr('src');
		var _thisSrc = $(this).attr('src');
		
		_mainImage.attr('src',_thisSrc.replace(/_thumb/,''));
		
		if (_mainSrc.slice(-5,-4) == ')')
		{
			// Main Image
			$(this).attr('src',_thisSrc.replace(/\d_thumb.jpg/,'1_thumb.jpg'));
		}
		else
		{
			var _test = _mainSrc.slice(-5,-4)+'_thumb.jpg';
			$(this).attr('src',_thisSrc.replace(/\d_thumb.jpg/,_mainSrc.slice(-5,-4)+'_thumb.jpg'));
		}
		
		return false;
	}).hover(function(){$(this).addClass('cursor');},function(){$(this).removeClass('cursor');});
});

$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}