	
	$(function() {
		
		/* Current page number */
		var page_number = 1;

		/* Total number of available pages */
		var page_count = $('DIV.product_container').length;

		/* Number of product images to be pre-loaded automatically on page change */
		var no_images_to_load = 20;

		
		/* Apply DBTips */
		$('DIV.product IMG').dbtips({
			text    : 'CLICK TO VIEW',
			offsetX : -10,
			offsetY : 40
		});

		/* Apply Cycle plugin for product listing */
		if ( $('#product_window .product_container').length > 1 ) {
			$('#product_window').cycle({
				fx     : 'scrollHorz',
				next   : '.next',
				prev   : '.prev',
				speed  : 'fast',
				timeout: 0,
				prevNextClick : setPageNumber,
				after  : loadImages
			});
			
			$('#product_window').css('margin-bottom', '0px');
		}
		else {
			loadImages();
		}

		/* Apply hover-over actions for product images */
		$('.imageCell').hover(function() {
				if($(this).find('a img').length > 1) {
					$(this).find('a img:eq(0)').hide();
					$(this).find('a img:eq(1)').show().css('display', 'block');
				}
			},
			function() {
				if($(this).find('a img').length > 1) {
					$(this).find('a img:eq(1)').hide();
					$(this).find('a img:eq(0)').show().css('display', 'block');
				}
		});
		
		$(document).keyup(function(event) {
			if(event.keyCode == 37) { // left arrow key
				$('.prev:eq(0)').trigger('click');
			} else if(event.keyCode == 39) { // right arrow key
				$('.next:eq(0)').trigger('click');
			}
		});
		
		// this function modifies the current page number
		function setPageNumber(e) {
			if(e) {
				// if 'next' clicked
				page_number++;
				if(page_number > page_count) {
					page_number = page_number%page_count;
				}
			} else {
				// if 'prev' clicked
				page_number--;
				if(page_number < 1) {
					page_number = page_number+page_count;
				}
			}
			
			$('.current_page').text(page_number);
		}

		/**
		 * Function to lazy load images
		 */
		function loadImages() {
			$('#product_window div.product_container').eq(page_number-1).find("img:lt(" + no_images_to_load + ")").each(function() {
				loadImage(this);
			});

			var list_of_images_to_load = $('#product_window div.product_container').eq(page_number-1).find("img[original!=]:gt(" + (no_images_to_load-1) + ")");
			var window_handler = function(event) {
				if ( list_of_images_to_load.length ) {
					list_of_images_to_load.each(function() {
						if ( $(this).attr("original") == undefined ) {
							/* Do nothing - image already loaded */
						} else if ( $(window).scrollTop() >= $(this).offset().top + $(this).height() ) {
							/* Do not load image if it's above visible section. */
						} else if ( $(window).height() + $(window).scrollTop() > $(this).offset().top ) {
							/* Load image if it's in visible section */
							loadImage(this);
						}
					});

					/* Remove image from array so it is not looped next time. */
					var temp = $.grep(list_of_images_to_load, function(element) {
						return $(element).attr("original") != undefined;
					});

					list_of_images_to_load = $(temp);
				}
				else {
					$(window).unbind('scroll', window_handler);
				}
			};

			/**
			 * Unbind previous scroll event and attach new one
			 */
			$(window).unbind('scroll', window_handler);
			if ( list_of_images_to_load.length ) {
				$(window).bind('scroll', window_handler);
			}
		}

		/**
		 * Function to lazy load one image
		 * @param	img
		 */
		function loadImage(img) {
			var self = img;
			
			/* Load original image */
			if ( $(self).attr("original") != undefined ) {
				/* If original attribute is empty - remove it */
				if ( ($(self).attr("original") == "") || ($(self).attr("original") == $(self).attr("src")) ) {
					$(self).removeAttr("original");
				}
				else if ( $(self).attr("lock") != undefined ) {
					/* Do nothing because image is currently loading */
				}
				else {
					/* Set 'lock' attribute to avoid sending many times same requests */
					$(self).attr("lock", "lock");
					
					$("<img />")
						.bind("load", function() {
							/* Double check if 'original' attribute is still set - in case if this image was loaded already */
							$(self)
								.attr("src", $(self).attr("original"))
								.removeAttr("original")
								.removeAttr("lock");
						})
						.attr("src", $(self).attr("original"));
				}
			}
		}
		
	});

//Set Variable for Event Tracking
category_page = true;

