/*!

* mkLightbox: a jQuery Plugin
* @author: Mike Hins ( www.rougemarketing.com )
* @published: 
* @updated: 
* @license Creative Commons Attribution Non-Commercial Share Alike 3.0 Licence http://creativecommons.org/licenses/by-nc-sa/3.0/
*
* @version 1.0
*
* TODO

*/
if (typeof jQuery != 'undefined') {
    jQuery(function ($) {
        $.fn.extend({
            mkLightbox: function (options) {
                var settings = $.extend({}, $.fn.mkLightbox.defaults, options);
                return this.each(function () {
                    // reference the container div
                    var $$ = $(this);
                    var o = $.metadata ? $.extend({}, settings, $$.metadata()) : settings;
                    var _winW = $(window).width();
                    var _winH = $(window).height();
                    var _overlay = $('<div id="mk_lightbox"></div>');
					var _closeBtn = $('<div id="mk_closeButton"></div>');
                    var _main = $('<div id="mk_lightbox_holder"></div>');
                    var _leftBtn = $('<div class="mklb_leftBtn"><img src="' + o.prev_btn + '" /></div>');
                    var _rightBtn = $('<div class="mklb_rightBtn"><img src="' + o.next_btn + '" /></div>');
                    var currentIndex = 0;
                    var _numOfItems = $$.find('.mob_cell').size();

                    $$.find('.mob_cell .mklb_link').each(function (i, el) {
                        $(this).bind("click", function (e) {
                            e.preventDefault();
                            currentIndex = $(this).parent().index('.mob_cell');
                            buildBG();
                        });
                    });

                    $(window).smartresize(function () {
						if($$.is(':visible'))
						{
							_winW = $(window).width();
	                    	_winH = $(window).height();
                        	destroy();						
                        	buildBG();
						}
                    });

                    function buildBG() {
                        _overlay.css({
                            'background': '#000',
                            'height': _winH,
                            'width': _winW,
                            'position': 'fixed',
                            'z-index': 99998,
                            'top': 0,
                            'left': 0,
                            'opacity': 0.7,
                            'display': 'none'
                        })
                        _main.css({
                            'background': '#000',
                            'height': 558,
                            'width': 508,
                            'position': 'fixed',
                            'z-index': 99999,
                            'top': (_winH - 558) / 2,
                            'left': (_winW - 508) / 2,
                            'display': 'none'
                        })

                        $('body').append(_overlay);
                        $('body').append(_main);

                        _overlay.fadeIn('fast', function () {
                            _main.fadeIn('fast', function () {
                                loadImage();
                            })
                        });
                        
						
						_main.append(_leftBtn);
                        _main.append(_rightBtn);
						_main.append(_closeBtn);

                        _overlay.click(function (e) {
                            destroy();
                        })
                    }

                    function loadImage() {
                        var currentItem = $$.find('.mob_cell').eq(currentIndex);
                        var _descr = currentItem.find('.mk_lightbox_holder').html();
                        var _picture = currentItem.find('img').attr('src');
						
						if(o.phpThumb_path != '')
						{
							var end = _picture.replace(/.+\.jpg/gi, '');
							var src = _picture.replace(o.phpThumb_path + "?src=", '').replace(end,'');
							_picture = o.phpThumb_path + "?src=" + src + o.phpThumb_params;
						}
                        _main.empty().append(_descr).append(_leftBtn).append(_rightBtn).append(_closeBtn).find('.mklb_background').css({
                            'background': 'url(' + _picture + ') no-repeat 0 0',
                            'width': 507,
                            'height': 384,
							'display' : 'none'
                        }).fadeIn();
                        _main.find('.mklb_content');

                        attachButtonBehavior();
                    }


                    function attachButtonBehavior() {
                        _leftBtn.click(function () {
                            checkIndex('prev');
                            loadImage();
                        })
                        _rightBtn.click(function () {
                            checkIndex('next');
                            loadImage();
                        })
						_closeBtn.click(function (e) {
                            destroy();
                        })
                    }

                    function checkIndex(direction) {
                        if (direction == 'next') currentIndex = (currentIndex + 1) < _numOfItems ? (currentIndex + 1) : 0;
                        if (direction == 'prev') currentIndex = (currentIndex) > 0 ? (currentIndex - 1) : _numOfItems - 1;
                        return currentIndex;
                    }

                    function destroy() {
                        _overlay.empty().remove();
                        _main.empty().remove();
                    }

                    function listlast(str, delimiter) {
                        var d = delimiter || '/';
                        return str.substr(str.lastIndexOf(delimiter) + 1);
                    }
                });
            }
        });
        $.fn.mkLightbox.defaults = {
            prev_btn: '',
            next_btn: '',
            big_picture_path: ''
        };
    }(jQuery));
}

(function($,sr){
 
  // debouncing function from John Hann
  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  var debounce = function (func, threshold, execAsap) {
      var timeout;
 
      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null; 
          };
 
          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);
 
          timeout = setTimeout(delayed, threshold || 100); 
      };
  }
	// smartresize 
	jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
 
})(jQuery,'smartresize');
