/*global jQuery */
/* ****************************************************************************

	CJ Object Scaler jQuery Plug-In v2.0

	This library is released under the BSD license:

	Copyright (c) 2008, Doug Jones. All rights reserved.
**************************************************************************** */
(function ($) {
	$.fn.cjObjectScaler = function (options) {

		/* 
			user variables (settings)
		***************************************/
		var settings = {
			// user editable settings
			destObj: null,
			// must be a jQuery object
			method: "fill",
			// fit|fill
			fade: 0 // if positive value, do hide/fadeIn
		};

		/* 
			system variables
		***************************************/
		var sys = {
			// function parameters
			version: '2.0.0',
			elem: null
		};

		/* 
			scale the image
		***************************************/
		function scale(self) {

			// declare some local variables
			var dest = settings.destObj,
			destW = $(dest).width(),
			destH = $(dest).height(),
			ratioX,
			ratioY,
			scale,
			newWidth,
			newHeight;

			// calculate scale ratios
			ratioX = destW / $(self).width();
			ratioY = destH / $(self).height();

			// Determine which algorithm to use
			if (!$(self).hasClass("cf_image_scaler_fill") && ($(self).hasClass("cf_image_scaler_fit") || settings.method === "fit")) {
				scale = ratioX < ratioY ? ratioX : ratioY;
			} else if (!$(self).hasClass("cf_image_scaler_fit") && ($(self).hasClass("cf_image_scaler_fill") || settings.method === "fill")) {
				scale = ratioX > ratioY ? ratioX : ratioY;
			}

			// calculate our new image dimensions
			newWidth = parseInt($(self).width() * scale, 10);
			newHeight = parseInt($(self).height() * scale, 10);

      if (newWidth > $(self).width() || newHeight > $(self).height()) {
        newWidth = $(self).width();
        newHeight = $(self).height();
      }

      if (settings.centreer == "center") {
  			$(self).css({
  				"width": newWidth + "px",
  				"height": newHeight + "px",
  				"position": "absolute",
  				"top": parseInt((destH - newHeight) / 2, 10) + "px",
  				"left": parseInt((destW - newWidth) / 2, 10) + "px"
  			}).attr({
  				"width": newWidth,
  				"height": newHeight
  			});      
      }

			// Set new dimensions & offset

			$(self).css({
				"width": newWidth + "px",
				"height": newHeight + "px",
				"position": "absolute",
				"top": parseInt((destH - newHeight) / 2, 10) + "px",
				"left": parseInt((destW - newWidth) / 2, 10) + "px"
			}).attr({
				"width": newWidth,
				"height": newHeight
			});

			// do our fancy fade in, if user supplied a fade amount
			if (settings.fade > 0) {
				$(self).fadeIn(settings.fade);
			}

		}

		/* 
			initialize the flipBox
		***************************************/
		function init() {

			var self = $(sys.elem)[0];

			// check to make sure we have a valid destination object
			if (settings.destObj === null || typeof settings.method !== "string" || typeof $(settings.destObj)[0] !== "object") {
				return;
			} else {

				// need to make sure the user set the parent's position
				// things go bonkers, if not set.
				if ($(settings.destObj).css("position") === "static") {
					$(settings.destObj).css({
						"position": "relative"
					});
				}

				// if the user supplied a fade amount, hide our image
				if (settings.fade > 0) {
					$(self).hide();
				}

				// run our scale function. Special case for images, we need
				// to make sure the image has loaded in order to get the width & height
				if ($(self)[0].nodeName === "IMG") {
					// check to see if the image is already loaded
					if ($(self).attr("complete")) {
						scale($(self)[0]);
					} else {
						$(self).load(function () {
							scale($(self)[0]);
						});
					}
				} else {
					scale($(self)[0]);
				}

			}
		}

		/* 
			set up any user passed variables
		***************************************/
		if (options) {
			$.extend(settings, options);
		}

		/* 
			main
		***************************************/
		return this.each(function () {
			sys.elem = this;
			init();
		});

	};
})(jQuery);

$(document).ready(function () {
  $('.out').click(function() { 
    var url = $.ajax({
      url: '/out/' + $(this).attr('id').substring(1),
      async: false
     }).responseText;
    window.open(url);
    return false;
  });

  $('.shopout').click(function() { 
    var url = $.ajax({
      url: '/shopout/' + $(this).attr('id').substring(1),
      async: false
     }).responseText;
    window.open(url);
    return false;
  });
  

  if ($('#offers .product').length < 9) {
    $('#showoffers').hide();
  }
  
  $(".photo").cjObjectScaler({
    destObj: $(".photo").parent().parent(), // must be a jQuery object (required)
    method: "fit", // can either be fit or fill (default fill)
    fade: 500 // if a positive integer, will hide and fadeIn object n duration (default 0)
  });
  
  $('#showoffers').click(function() { 
    $('#offers').animate({height : '100%'}, {duration: 1000});
  });
});
