/*
 * jQuery WG PhotoFramer plug-in 1.0
 * Copyright (c) 2008 Roberto Lee (webgenerator.nl)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Date: 2009-08-13
 * Rev: 1
 */
 (function($){
     $.fn.extend({
         WG_PhotoFramer: function(options) {
	        var defaults = {
				frametype: 'polaroid', 							//polaroid, polaroid_with_text, frame_image (the width and height of this image must be larger then the width and height of the photoalbum images)
				move_result_to_container: '',
				unwrap_from_parent : false,
				frame_img: 'templates/_helpers/jquery.wg_photo_framer_2.png',
				center_photo: true,
				tranparent_img: 'images/transparent.gif',
				margin_between_framed_photos: '0px 5px 5px 0px',
				frame_width: 14,
				frame_color: '#fff',
				frame_shadow_width: 1,
				frame_shadow_color: '#A9A9A9', //OR ARRAY EX      ['#EEE','#A9A9A9','#A9A9A9','#EEE']
				photoframed_max_width: 100,
				photoframed_max_height: 100,
				polaroidtext_height: 13,
				polaroidtext_line_height: 10,
				polaroidtext_txt_align: 'center',
				polaroidtext_font_size: 10,
				cb: function(){}
	        };
	        var options = $.extend(defaults, options);

            return $(this).each(function(idx) {
				if($(this))
				{
					var parentholder = $(this).parents("div").get(0);

					var imgsrc = $(this).attr('src');
					var photo_size_width = options.photoframed_max_width - options.frame_shadow_width - (options.frame_width * 2);
					var photo_size_height = options.photoframed_max_height - options.frame_shadow_width - (options.frame_width * 2);

					//REPLACE IMAGE IMMEDIATELY WITH TRANSPARENT IMAGE
					if(options.center_photo)
					{
						$(this).attr('src', options.tranparent_img).css({
							'width' : photo_size_width + 'px',
							'height' : photo_size_height + 'px'
						});
					}

					var css_photoframe;
					var css_photoframe_photo;
					var css_photoframe_text;

					var is_array_option_frame_shadow_color = $.isArray(options.frame_shadow_color);
					css_photoframe = {
										'position': 'relative',
										'float': 'left',
										'margin': options.margin_between_framed_photos,
										'border-style' : 'solid',
										'border-width' : options.frame_shadow_width+'px',
										'border-top-color' : is_array_option_frame_shadow_color ? options.frame_shadow_color[0] : options.frame_shadow_color,
										'border-right-color' : is_array_option_frame_shadow_color ? options.frame_shadow_color[1] : options.frame_shadow_color,
										'border-bottom-color' : is_array_option_frame_shadow_color ? options.frame_shadow_color[2] : options.frame_shadow_color,
										'border-left-color' : is_array_option_frame_shadow_color ? options.frame_shadow_color[3] : options.frame_shadow_color,
										'width' : (options.photoframed_max_width - options.frame_shadow_width) + 'px',
										'height' : (options.photoframed_max_height - options.frame_shadow_width) + 'px'
									};

					css_photoframe_photo = {
										'overflow' : 'hidden',
										'position': 'relative',
										'float': 'left',
										'border': options.frame_width + 'px solid #fff',
										'width' : photo_size_width + 'px',
										'height' : photo_size_height + 'px',
										'background-repeat' : 'no-repeat',
										'background-position' : '50% 50%',
										'background-color':'#FFF',
										'background-image' : 'url('+ (options.center_photo ?  imgsrc : options.tranparent_img) +')'
									};

					css_photoframe_text = {
										'overflow' : 'hidden',
										'position': 'relative',
										'float': 'left',
										'border-width' : '0px ' + options.frame_width + "px " + options.frame_width + "px " + options.frame_width + "px ",
										'border-style':'solid',
										'border-color': options.frame_color,
										'width' : photo_size_width + 'px',
										'height' : options.polaroidtext_height + 'px',
										'line-height' : options.polaroidtext_line_height + 'px',
										'text-align' : options.polaroidtext_txt_align,
										'font-size' : options.polaroidtext_font_size + 'px'
									};

					css_imageframe_corner = {
										'overflow' : 'hidden',
										'position': 'relative',
										'float': 'left',
										'border-width' : '0px',
										'width' : (options.photoframed_max_width/2) + 'px',
										'height' : options.frame_width + 'px',
										'background-repeat' : 'no-repeat'
									};

					css_imageframe_sides = {
										'overflow' : 'hidden',
										'position': 'relative',
										'float': 'left',
										'border-width' : '0px',
										'width' : options.frame_width + 'px',
										'height' : (options.photoframed_max_height - (options.frame_width * 2)) + 'px',
										'background-repeat' : 'no-repeat'
									};

					var has_link = $($(this).parent("a").get(0)).is('*'); //CHECK IF IMAGE IS WRAPPED IN LINK TAG

					switch(options.frametype)
					{
						case 'polaroid':
							//PREPARE WRAPPER DIVS
							var photo_frame = $("<div/>").addClass('photoframe').css(css_photoframe);
							var photoframe_photo = $("<div/>").addClass('photoframe_photo').css(css_photoframe_photo);
							if(has_link)
							{
								$($(this).parent("a").get(0)).wrap(photo_frame).wrap(photoframe_photo);
							}
							else
							{
								$(this).wrap(photo_frame).wrap(photoframe_photo);
							}
							break;
						case 'polaroid_with_text':
							jQuery.extend(css_photoframe_photo, {
																	'border' : options.frame_width + 'px solid ' + options.frame_color,
																	'height' : (photo_size_height - options.polaroidtext_height - options.frame_width) + 'px'
																});
							//PREPARE WRAPPER DIVS
							var photo_frame = $("<div/>").addClass('photoframe').css(css_photoframe);
							var photoframe_photo = $("<div/>").addClass('photoframe_photo').css(css_photoframe_photo);
							if(has_link)
							{
								$($(this).parent("a").get(0)).wrap(photo_frame).wrap(photoframe_photo);
							}
							else
							{
								$(this).wrap(photo_frame).wrap(photoframe_photo);
							}
							//PREPARE TEXT DIV
							var photoframe_text = $("<div/>").addClass('photoframe_text').css(css_photoframe_text);
							$($(this).parents(".photoframe").get(0)).append(photoframe_text.html($(this).attr('title')));
							break;
						case 'frame_image':
							//NO SHADOW
							photo_size_width = options.photoframed_max_width - (options.frame_width * 2);
							photo_size_height = options.photoframed_max_height - (options.frame_width * 2);


							jQuery.extend(css_photoframe, {
															'border-width' : '0px',
															'width' : options.photoframed_max_width + 'px',
															'height' : options.photoframed_max_height + 'px'
														  });

							jQuery.extend(css_photoframe_photo, {
																	'border-width' : '0px',
																	'width' : photo_size_width + 'px',
																	'height' : photo_size_height + 'px'
																});

							$.extend(css_imageframe_corner, {'background-image' : 'url('+ options.frame_img +')'});
							$.extend(css_imageframe_sides, {'background-image' : 'url('+ options.frame_img +')'});

							var frame_sides_left = $("<div/>").addClass('frame_side').css($.extend(css_imageframe_sides, {'background-position' : '0% -' + options.frame_width + 'px'}));
							var frame_sides_right = $("<div/>").addClass('frame_side').css($.extend(css_imageframe_sides, {'background-position' : '100% -' + options.frame_width + 'px'}));

							var frame_corner_topleft = $("<div/>").addClass('frame_corner').css($.extend(css_imageframe_corner, {'background-position' : '0% 0%'}));
							var frame_corner_topright = $("<div/>").addClass('frame_corner').css($.extend(css_imageframe_corner, {'background-position' : '100% 0%'}));
							var frame_corner_bottomleft = $("<div/>").addClass('frame_corner').css($.extend(css_imageframe_corner, {'background-position' : '0% 100%'}));
							var frame_corner_bottomright = $("<div/>").addClass('frame_corner').css($.extend(css_imageframe_corner, {'background-position' : '100% 100%'}));

							var photo_frame = $("<div/>").addClass('photoframe').css(css_photoframe);
							var photoframe_photo = $("<div/>").addClass('photoframe_photo').css(css_photoframe_photo);
							if(has_link)
							{
								$($(this).parent("a").get(0)).wrap(photo_frame).wrap(photoframe_photo);
							}
							else
							{
								$(this).wrap(photo_frame).wrap(photoframe_photo);
							}

							$($(this).parents(".photoframe").get(0)).prepend(frame_sides_left)
																	.prepend(frame_corner_topright)
																	.prepend(frame_corner_topleft)
																	.append(frame_sides_right)
																	.append(frame_corner_bottomleft)
																	.append(frame_corner_bottomright);
							break;
					}

					if((options.move_result_to_container != '') && ($(options.move_result_to_container).is('*')))
					{
						$(options.move_result_to_container).append($(this).parents(".photoframe").get(0));
					}

					if(options.unwrap_from_parent)
					{
						$($(parentholder).parents("div").get(0)).append($(this).parents(".photoframe"));
					}
					options.cb.call();
				}

            });
		}
    });
})(jQuery);


(function($) {

   $.fn.log = function(msg) {
      if (typeof (console) == "undefined") {
         console = { log: function() { } };
      }
      if (console) {
         console.log("%s: %o", msg, this);
      }
      return this;
   }
})(jQuery);

jQuery.log = function(msg) {
      if (typeof (console) == "undefined") {
         console = { log: function() { } };
      }
      if (console) {
         console.log("%s: %o", msg, this);
      }
};


(function($) {
   $.fn.WG_SizeElemAsElem = function(elem2imitate) {
		return this.each(function() {
			$(this).css({
						'height': $(elem2imitate).height() + 'px',
						'width': $(elem2imitate).width() + 'px'
					})
		});
   }
})(jQuery);

(function($) {
   $.fn.WG_CenterText = function() {
		return this.each(function() {
			var pos_top_txt = ($(elem).height() - $(this).height())/2;
			$(this).css({
						'text-align': 'center',
						'top': pos_top_txt + 'px',
						'position': 'absolute',
						'left': '0px',
						'width': $(this).width() + 'px'
					})
		});
   }
})(jQuery);


