/**/
/*
* cmo: a jQuery Plugin
* @author: Mike Hins ( www.tremblant.ca )
* @url: http://www.tremblantmedias.com/cmo/ressources/javasacript/jquery.mike.cmo.js
* @documentation: http://www.tremblantmedias.com/cmo/ressources/javasacript/jquery.mike.cmo.js
* @published: 
* @updated: 
* @license Creative Commons Attribution Non-Commercial Share Alike 3.0 Licence http://creativecommons.org/licenses/by-nc-sa/3.0/
*
* @version 4.0
*
*/
if (typeof jQuery != 'undefined') {
    jQuery(function ($) {
        $.fn.extend({
            cmo: function (options) {
                var settings = $.extend({}, $.fn.cmo.defaults, options);
                return this.each(function () {
                    // reference the container div
                    var $$ = $(this);
                    var o = $.metadata ? $.extend({}, settings, $$.metadata()) : settings;
                    var $LST = $("<div />");
                    // Number of tremblant Suits offer
                    var lstOffersNum = o.lstOffersNum;
                    // array that hold temporary datas
                    var prices = [];
                    // we sort the last minute offer by [ price | date ]
                    var sortBy = "price";
                    // object that hold the datas
                    var data = {};
                    //
                    var _from 	= cmo_lang == 'fr' ? 'Du' : 'From';
                    var _to 	= cmo_lang == 'fr' ? 'au' : 'to';
                    var _view 	= cmo_lang == 'fr' ? 'voir l\'offre' : 'view offer';
                    var _price 	= cmo_lang == 'fr' ? 'À partir de' : 'Starting from';
                    var _last 	= cmo_lang == 'fr' ? 'Offre de dernière minute' : 'Last Mininute Offer';
                    // calling the webservice

                    function callService() {
                        $.getJSON(o.url, function (d) {
                            data = d;
                            buildPanel();
                        })
                    }

                    function buildPanel() {
                        for (var i = 0; i < data.length; i++) {
                            // populate the prices array so we can sort that div at the end
                            prices[i] = {
                                'price': data[i].price,
                                'idx': i,
                                end_date: data[i].end_date
                            };
                            // html markup off the offer holder
                            var markup = '<div class="cmo_holder clearfix"> \
												<div class="cmo_img"> \
													<img src="' + o.image_path + data[i].image_url + '" /> \
												</div> \
												<div class="cmo_text"> \
													<h3><a class="cmo_title" href="' + data[i].link + '&source_id=3100">' + data[i].vendor_name + '</a></h3> \
													<p class="cmo_desc">' + data[i].descr + '</p> \
													<p class="cmo_date" rel="' + data[i].start_date + '|' + data[i].end_date + '">' + _from + ' ' + data[i].f_start_date + ' ' + _to + ' ' + data[i].f_end_date + '</p> \
												</div> \
												<div class="cmo_price"> \
													<p class="cmo_price_price">' + data[i].price + '</p> \
													<a class="cmo_price_link" href="' + data[i].link + '&source_id=3100">' + _view + '</a> \
												</div> \
											</div>';
                            // if the mg_code == 74 = Tremblant suits
							if(data[i].mg_code == 74)
	                            $LST.append(markup)
                        }
                        // randomize the offers
                        if (o.randomize) {
                            $LST = $LST.shuffle();
                            $Partners = $Partners.shuffle();
                        }                       
                        // Hide loading gif.
                        $("#cmo_loading_img").remove();
                        // We build the panel						
                        $LST.find('.cmo_holder').each(function (i, el) {
                            if (i >= lstOffersNum) return false;
                            $$.append($(this));
                        })                        
                    }
                    callService();
                });
            }
        });
		/*
		* jQuery shuffle
		*
		* Copyright (c) 2008 Ca-Phun Ung <caphun at yelotofu dot com>
		* Dual licensed under the MIT (MIT-LICENSE.txt)
		* and GPL (GPL-LICENSE.txt) licenses.
		*
		* http://yelotofu.com/labs/jquery/snippets/shuffle/
		** Shuffles an array or the children of a element container.* This uses the Fisher-Yates shuffle algorithm <http://jsfromhell.com/array/shuffle [v1.0]>
		*/
        $.fn.shuffle = function () {
            return this.each(function () {
                var items = $(this).children().clone(true);
                return (items.length) ? $(this).html($.shuffle(items)) : this;
            });
        }
        $.shuffle = function (arr) {
            for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
            return arr;
        }
        $.fn.cmo.defaults = {
            url: "",
            sortBy: "",
            lstOffersNum: 2,
            randomize: true,
            routage: '',
            image_path: ''
        };
    }(jQuery));
}
