// The function that initializes the page
$(document).ready(function()
{
    // Initialize visuals
    parseVisuals();

    // Initialize language hovers
    initLangHover();

    // Initialize sIFR
    initSifr();

    // Initialize product photos
    initProductPhotos();

    // Initialize tabs
    initTabs();

    positionBuyMore();

    checkout.init();
    giftMessage.init();

    // Trap Context menu on visuals
    detectClick();

    initRelExternal();
});

/**
 * Initialize the form
 * 
 * @author Davy De Pauw
 */

parseForm = function() {
	// Listen for focus on specified input fields
	$("dd.focus > input").focus(function() {
		toggleFocusVisibility(this);
	}),
	
	$("dd.focus > input").blur(function() {		
		toggleFocusVisibility(this);
	}),
	
	toggleFocusVisibility = function(target) {
		var parent = $(target).parent().get(0);
		
		// Attach visible class
		$(parent).find('div.note').toggleClass('hidden');		
	}
}

/**
 * Parse all search boxes with id in the given array
 * Listens to onfocus and onblur events
 * 
 * @param {Array} boxes
 * @author Davy De Pauw
 */

parseVisuals = function() {
	// waarom was dit aangepast naar:
	//$('.category .visual').hover ?
	// heb het terug naar oorsprokelijke code aangepast
	$('.visual').hover (
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);

	$('.thumb').hover (
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);				

	$('#cart-add div').hover (
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);

}

/**
 * Shows or hides the initial value text depending onfocus or onblur
 * 
 * @param {Object} e
 * @param {Object} obj
 * @author Davy De Pauw
 */

processTextarea = function(e, obj) {
	// Which type of event is fired?	
	switch(e.type) {
		
		case 'focus':
			// Hide the initialValue
			if(e.target.value == obj.initialValue) { e.target.value = ''; }
			break;
			
		case 'blur':
			// Show the initialValue
			if(e.target.value == '') { e.target.value = obj.initialValue; }
			break;
	}	
}

initLangHover = function() {
	$('#lang li').hover (
		function() {
			var className = $(this).get(0).id;
			$(this).parent().get(0).className = className;			
		},
		function() {
			var className = 'lang-' + $('#lang-current').get(0).value;
			$(this).parent().get(0).className = className;		
		}
	)
}

initSifr = function() {
	if (typeof sIFR == "function") {
		sIFR.replaceElement(".sifr", named({
			sFlashSrc: "/fonts/subaccuz.swf",
			sColor: "#51bdcd",
			sCase: "lower",
			sWmode: "transparent"
		}));

		sIFR.replaceElement(".sifr-section", named({
			sFlashSrc: "/fonts/subaccuz.swf",
			sColor: "#754125",
			sCase: "lower",
			sWmode: "transparent"
		}));

		sIFR.replaceElement(".sifr-3", named({
			sFlashSrc: "/fonts/subaccuz.swf",
			sColor: "#93d1d1",
			sCase: "lower",
			sWmode: "transparent"
		}));

		sIFR.replaceElement(".sifr-4", named({
			sFlashSrc: "/fonts/subaccuz.swf",
			sColor: "#e60404",
			sWmode: "transparent"
		}));

		sIFR.replaceElement(".sifr-5", named({
			sFlashSrc: "/fonts/subaccuz.swf",
			sColor: "#ffffff",
			sCase: "lower",
			sWmode: "transparent"
		}));

	}
};

positionBuyMore = function()
{
    if ($('#buy-more').length > 0)
    {
        // number of items
        var numitems = $('#basket-body').height() / 54;

        if (numitems > 3)
        {
            // 27 = height red box / 2
            // Get the y pos of total relative to content div
            var yPos = ($('#checkout-total').offset().top - $('body').offset().top);
            var centerPos = yPos + ($('#checkout-total').height() / 2);
            var position = centerPos - ($('#buy-more').height() / 2);

            $('#buy-more').css("top", position + 'px');
        }
        else
        {
            $('#buy-more').removeAttr("style");
        }
    }
}

initTabs = function() {

	if($('#tabs').length > 0)
	{
	    if(typeof($('#tabs').tabs) != 'undefined')
	    {
	        $('#tabs').tabs();
	    }
	}
}

initProductPhotos = function()
{
    $('#photos ol li a').each(function(i)
    {
        $('span:last', $(this).get(0)).get(0).innerHTML = (i + 1);

        $(this).hover(function(e)
        {
            resetProductPhotosActiveStates();

            $(this).parent().get(0).className = "selected";

            // Set src of #photo to href of clicked item
            $('#photo').attr('src', $(this).attr('href'));
        }, function(e)
        {
            //Do nothing, stay there
        });

        $(this).click(function(e)
        {
            e.preventDefault();

            resetProductPhotosActiveStates();

            $(this).parent().get(0).className = "selected";

            // Set src of #photo to href of clicked item
            $('#photo').attr('src', $(this).attr('href'));

        });
    });

};

resetProductPhotosActiveStates = function() {
	// Get all li items in specified ol
	$('#photos ol li').each(function(i) {
		this.className = '';
	});
};

/**
 * Checkout
 * 
 * All functionaly needed on the checkout forms.
 * *********************************************************************************************
 * 
 * @author Davy De Pauw
 */

checkout = {
	init: function() {
		
		
		// If btn-verificationcode exists on page
		if($('#btn-verificationcode').length > 0) {
			this.toggleHelpPopup($('#btn-verificationcode'));
		}
	},
	
	toggleHelpPopup: function(button) {
		button.click(function() {
			var helpPopup = $('#help-verificationcode');
			if(helpPopup.length > 0) {
				// Toggle visibility
				helpPopup.toggleClass('hidden');
			}
		});
	}
}



/**
 * Checkout
 * 
 * Gift message counter
 * *********************************************************************************************
 * 
 * @author Davy De Pauw
 */

giftMessage = {

	init: function() {
		var messageArea = $('#gift-message');
		
		if(messageArea.length > 0) {
			messageArea.keyup(function(event) {
				giftMessage.updateTextCharCounter(event.target.value);
			});			
		}
	},
		
	/**
	 * Updates the number of characters left to type in the twitterstyle textarea
	 * 
	 * @param {Object} value
	 */
	
	updateTextCharCounter: function(value) {
		$('#message-counter-value').get(0).innerHTML = 200 - value.length;
		
		var counter = $('#message-counter');
		
		if (value.length > 190) {
			counter.css('color', '#e60404');
	    } else if (value.length > 180) {
			counter.css('color', '#754125');
	    } else {
			counter.css('color', '#93D1D1');
	    }
	}
}

/**
* overall
* 
* Detect a click on a product visual on the product detail view
* *********************************************************************************************
* 
* @author Vincent Mouton
*/

detectClick = function() {
    if ($('#photos .meta').length > 0)
    {
        $('#photos .meta')[0].oncontextmenu = function()
        {
            return false;
        }
    }
}

/**
* overall
* 
* Detect a click on a <a/> with rel="external" and open in a new window
* *********************************************************************************************
* 
* @author Vincent Mouton
*/
initRelExternal = function()
{
    $('a[rel=external]').each(function()
    {
        $(this).click(function()
        {
            window.open($(this).attr('href'));
            return false;
        });
    });
}


/**
* overall
* 
* Get a resource
* *********************************************************************************************
* 
* @author Vincent Mouton
*/
getResource = function(key)
{
    var result = '';
    if ($(resources).length > 0)
    {
        $(resources).each(function()
        {
            if (this.Key == key)
            {
                result = this.Value;
                return;
            }
        });
    }

    return result;
}
