﻿var jQueryScriptOutputted = false;
function initJQuery() {
    //if the jQuery object isn't available
    if (typeof (jQuery) == 'undefined') {
        if (!jQueryScriptOutputted) {
            //only output the script once..
            jQueryScriptOutputted = true;

            //output the script (load it from google api)
//            document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js\"></scr" + "ipt>");
            document.write("<scr" + "ipt type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js\"></scr" + "ipt>");
        }
        setTimeout("initJQuery()", 50);
    } else {
        var oTimeout;
        var bIsOver = false;

        (function($) {
            $["Global"] = {
                baseURL: "http://kaluxo.remax-quebec.com/ext/flippers",
                alsURL: "http://als.id-3.net/EntityPropertyListing.axd",
                cssURL: false,
                id: 0,
                Data: [],
                CurrentIndex: 0,
                MainWrapper: false,
                ImageWrapper: false,
                InfoWrapper: false,
                ThumbWrapper: false,
                NextWrapper: false,
                PreviousWrapper: false,
                sold_label: 'Vendu',
                title_label: 'Propriétés Vedettes',
                detail_label: 'plus d\'info',
                start_at_index: 0,
                thumb_opacity: 0.7,
                animate_first_image: false,
                animation_speed: 200,
                image_width: 600,
                image_height: 600,
                image_fit_to: 'both',
                thumb_width: 70,
                thumb_height: 50,
                thumb_margin: 2,
                thumb_nb_visible: 4,
                thumb_detail_onclick: false,
                display_next_and_prev: true,
                display_back_and_forward: true,
                autostart: true,
                speed: 5000,
		order_by: false,              
                order_dir: false               
            };

            // *****************************
            // CHIC : START
            // *****************************
            $.fn.Chic = function(options) {
                $["Global"] = $.extend($.Global, options);

                if ($.Global.cssURL) {
                    $('<link rel="stylesheet" type="text/css" href="' + $.Global.cssURL + '">').appendTo('head');
                } else {
                    $('<link rel="stylesheet" type="text/css" href="' + $.Global.baseURL + '/css/chic.css">').appendTo('head');
                }

                $.Global.MainWrapper = $(this);

                $.ajax({
                    type: "POST",
                    url: $.Global.alsURL + "?id=" + $.Global.id + "&mod=json",
                    dataType: "jsonp",
                    timeout: 10000,
                    success: function(data) {
                        $.Global.Data = data
                        $(this).initControls($.Global.MainWrapper)
                        $(this).renderCurrent($.Global.MainWrapper, $.Global.Data);
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError);
                    }
                });

                $.fn.initControls = function() {

                    $('<table style="width:100%;" cellpadding="0" cellspacing="0" border="0">' +
                        '<tr>' +
                            '<td align="left" id="ad-previous-wrapper"></td>' +
                            '<td align="left" id="ad-wrapper-left"></td>' +
                            '<td align="left" id="ad-wrapper-right"></td>' +
                            '<td align="right" id="ad-next-wrapper"></td>' +
                        '</tr>' +
                      '</table>').appendTo($.Global.MainWrapper);

                    $.Global.ImageWrapper = $.Global.MainWrapper.find('#ad-wrapper-left');
                    $.Global.InfoWrapper = $.Global.MainWrapper.find('#ad-wrapper-right');
                    $.Global.ImageWrapper.css('width', $.Global.image_width);
                    $.Global.InfoWrapper.css('width', $.Global.image_width);

                    $('<div id="ad-loading-wrapper"></div>').appendTo($.Global.ImageWrapper);
                    $('<div id="ad-loading"></div>').appendTo($('#ad-loading-wrapper'));
                    $('#ad-loading').css('width', $.Global.image_width);
                    $('#ad-loading').css('height', $.Global.image_height);

                    $('<div id="ad-previous-image"></div>').appendTo('#ad-previous-wrapper');
                    $('<div id="ad-next-image"></div>').appendTo('#ad-next-wrapper');
                    $('#ad-previous-image').css('height', $.Global.image_height);
                    $('#ad-next-image').css('height', $.Global.image_height);

                    $('<div id="ad-image-wrapper"></div>').appendTo($.Global.ImageWrapper);
                    $('#ad-image-wrapper').css('width', $.Global.image_width);
                    $('#ad-image-wrapper').css('height', $.Global.image_height);
                    $('<a id="ad-image-link" target="_blank"></a>').appendTo('#ad-image-wrapper');
                    $('<img id="ad-image" border="0">').appendTo($('#ad-image-link'));

                    $('<div id="Title"></div>').appendTo($.Global.InfoWrapper);
                    $.Global.InfoWrapper.find('#Title').append($.Global.title_label);
                    $('<div id="City"></div>').appendTo($.Global.InfoWrapper);
                    $('<div id="Genre"></div>').appendTo($.Global.InfoWrapper);
                    $('<div id="Price"></div>').appendTo($.Global.InfoWrapper);

                    $('#ad-next-wrapper').find('#ad-next-image').click(
                        function() {
                            $(this).moveNext();
                            return false;
                        }
                    )
                    $('#ad-previous-wrapper').find('#ad-previous-image').click(
                        function() {
                            $(this).movePrevious();
                            return false;
                        }
                    )

                    $.Global.InfoWrapper.mouseover(
                        function() {
                            bIsOver = true;
                            return false;
                        }
                    );
                    $.Global.InfoWrapper.mouseout(
                        function() {
                            bIsOver = false;
                            return false;
                        }
                    );

                    $.Global.ImageWrapper.mouseover(
                        function() {
                            bIsOver = true;
                            return false;
                        }
                    );
                    $.Global.ImageWrapper.mouseout(
                        function() {
                            bIsOver = false;
                            return false;
                        }
                    );


                };

                $.fn.renderCurrent = function() {
                    $('#ad-image-link').find('#ad-image').remove();
                    $('<img id="ad-image" border="0">').appendTo($('#ad-image-link'));

                    $('#ad-image-wrapper').find('#ad-image-link').attr('href', $.Global.Data[$.Global.CurrentIndex].DetailURL);
                    $('#ad-image-link').find('#ad-image').attr('src', $.Global.Data[$.Global.CurrentIndex].PictureURL);

                    if ($.Global.Data[$.Global.CurrentIndex].Price == "0") {
                        $('<div id="ad-image-sold">&nbsp;</div>').appendTo('#ad-image-wrapper');
                        $('#ad-image-sold').css('width', $.Global.image_width + 'px');
                        $('#ad-image-sold').css('height', $.Global.image_height + 'px');
                    } else {
                        $('#ad-image-sold').remove();
                    };

                    $.Global.InfoWrapper.find('#City').empty();
                    $.Global.InfoWrapper.find('#City').append($.Global.Data[$.Global.CurrentIndex].City);
                    $.Global.InfoWrapper.find('#Genre').empty();
                    $.Global.InfoWrapper.find('#Genre').append($.Global.Data[$.Global.CurrentIndex].Genre);
                    $.Global.InfoWrapper.find('#Price').empty();
                    if ($.Global.Data[$.Global.CurrentIndex].Price != "0") {
                        $.Global.InfoWrapper.find('#Price').append($.Global.Data[$.Global.CurrentIndex].Price);
                    } else {
                        $.Global.InfoWrapper.find('#Price').append($.Global.sold_label);
                    }
                    if (!isNaN($.Global.Data[$.Global.CurrentIndex].Price.replace(' ', '')) && $.Global.Data[$.Global.CurrentIndex].Price != "0") {
                        $.Global.InfoWrapper.find('#Price').append(' $');
                    }
                    $.Global.InfoWrapper.find('#ad-detail').remove();
                    $('<div id="ad-detail"></div>').appendTo($.Global.InfoWrapper);
                    $('<a id="ad-detail-link" target="_blank"></a>').appendTo('#ad-detail');
                    $('#ad-detail-link').attr('href', $.Global.Data[$.Global.CurrentIndex].DetailURL);
                    $('#ad-detail-link').append($.Global.detail_label);

                    $.Global.ImageWrapper.find('#ad-image-wrapper').fadeTo($.Global.animation_speed, 1);

                    $('#ad-loading-wrapper').css('display', 'none');

                    //alert($.Global.MainWrapper.html());

                    if ($.Global.autostart) {
                        $(this).moveAuto();
                    };
                };

                $.fn.movePrevious = function() {
                    $('#ad-loading-wrapper').css('display', 'block');
                    window.clearInterval(oTimeout);
                    $.Global.CurrentIndex -= 1;
                    if ($.Global.CurrentIndex < 0) {
                        $.Global.CurrentIndex = $.Global.Data.length - 1;
                    }
                    //$.Global.ImageWrapper.find('#ad-image-wrapper').fadeTo($.Global.animation_speed, 0, function() { renderCurrent(); });
                    $.Global.ImageWrapper.find('#ad-image').animate({ "right": "-=" + $.Global.image_width + "px" }, $.Global.animation_speed, function() { $(this).showPrevious(); });
                }

                $.fn.moveNext = function() {
                    if (!bIsOver) {
                        $('#ad-loading-wrapper').css('display', 'block');
                        window.clearInterval(oTimeout);
                        $.Global.CurrentIndex += 1;
                        if ($.Global.CurrentIndex > $.Global.Data.length - 1) {
                            $.Global.CurrentIndex = 0;
                        }
                        //$.Global.ImageWrapper.find('#ad-image-wrapper').fadeTo($.Global.animation_speed, 0, function() { renderCurrent(); });
                        $.Global.ImageWrapper.find('#ad-image').animate({ "left": "-=" + $.Global.image_width + "px" }, $.Global.animation_speed, function() { $(this).showNext(); });
                    }
                }

                $.fn.showPrevious = function() {
                    $(this).renderCurrent();
                    $.Global.ImageWrapper.find('#ad-image').css('right', $.Global.image_width);
                    $.Global.ImageWrapper.find('#ad-image').animate({ "right": "-=" + $.Global.image_width + "px" }, $.Global.animation_speed);
                }
                $.fn.showNext = function() {
                    $(this).renderCurrent();
                    $.Global.ImageWrapper.find('#ad-image').css('left', $.Global.image_width);
                    $.Global.ImageWrapper.find('#ad-image').animate({ "left": "-=" + $.Global.image_width + "px" }, $.Global.animation_speed);
                }

                $.fn.moveAuto = function() {
                    window.clearInterval(oTimeout);
                    oTimeout = setInterval("$(this).moveNext();", $.Global.speed);
                }
            };

            // *****************************
            // CHIC : STOP
            // *****************************

            // *****************************
            // CLASSIC : START
            // *****************************
            $.fn.Classic = function(options) {
                $["Global"] = $.extend($.Global, options);

                if ($.Global.cssURL) {
                    $('<link rel="stylesheet" type="text/css" href="' + $.Global.cssURL + '">').appendTo('head');
                } else {
                    $('<link rel="stylesheet" type="text/css" href="' + $.Global.baseURL + '/css/classic.css">').appendTo('head');
                }

                $.Global.MainWrapper = $(this);

                $.ajax({
                    type: "POST",
                    url: $.Global.alsURL + "?id=" + $.Global.id + "&mod=json",
                    dataType: "jsonp",
                    timeout: 10000,
                    success: function(data) {
                        $.Global.Data = data
                        $(this).initControls($.Global.MainWrapper)
                        $(this).renderCurrent($.Global.MainWrapper, $.Global.Data);
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError);
                    }
                });

                $.fn.initControls = function() {
                    $.Global.MainWrapper.css('width', $.Global.image_width + 40);

                    $('<div id="ad-wrapper"></div>').appendTo($.Global.MainWrapper);
                    $.Global.ImageWrapper = $.Global.MainWrapper.find('#ad-wrapper');
                    $('<div id="ad-loading"></div>').appendTo($.Global.ImageWrapper);
                    $('#ad-loading').css('width', $.Global.image_width);
                    $('#ad-loading').css('height', $.Global.image_height);
                    $('<div id="ad-image-wrapper"></div>').appendTo($.Global.ImageWrapper);
                    $('#ad-image-wrapper').css('width', $.Global.image_width);
                    $('#ad-image-wrapper').css('height', $.Global.image_height);
                    $('<a id="ad-image-link" target="_blank"></a>').appendTo('#ad-image-wrapper');
                    $('<img id="ad-image" border="0">').appendTo($('#ad-image-link'));

                    $('<div id="controls"></div>').appendTo($.Global.ImageWrapper);
                    $('#controls').css('width', $.Global.image_width);

                    $('<table style="width:100%;" cellpadding="0" cellspacing="0" border="0"><tr><td align="left" id="ShowPrevious"></td><td align="left" id="City"></td><td align="right" id="Price"></td><td id="ShowNext"></td></tr></table>').appendTo('#controls');
                    $('<div id="Description"></div>').appendTo('#controls');

                    $.Global.ImageWrapper.find('#ShowNext').click(
                        function() {
                            $(this).moveNext();
                            return false;
                        }
                    )
                    $.Global.ImageWrapper.find('#ShowPrevious').click(
                        function() {
                            $(this).movePrevious();
                            return false;
                        }
                    )
                    $('#ad-image-wrapper').mouseover(
                        function() {
                            bIsOver = true;
                            return false;
                        }
                    );
                    $('#ad-image-wrapper').mouseout(
                        function() {
                            bIsOver = false;
                            return false;
                        }
                    );

                    $('#Description').mouseover(
                        function() {
                            bIsOver = true;
                            return false;
                        }
                    );
                    $('#Description').mouseout(
                        function() {
                            bIsOver = false;
                            return false;
                        }
                    );
                };

                $.fn.renderCurrent = function() {
                    $.Global.ImageWrapper.find('#ad-image').remove();
                    $('<img id="ad-image" border="0">').appendTo($('#ad-image-link'));

                    $.Global.ImageWrapper.find('#ad-image-link').attr('href', $.Global.Data[$.Global.CurrentIndex].DetailURL);
                    $.Global.ImageWrapper.find('#ad-image').attr('src', $.Global.Data[$.Global.CurrentIndex].PictureURL);

                    if ($.Global.Data[$.Global.CurrentIndex].Price == "0") {
                        $('<div id="ad-image-sold"></div>').appendTo('#ad-image-wrapper');
                        $('#ad-image-sold').attr('width', $.Global.image_width);
                        $('#ad-image-sold').attr('height', $.Global.image_height);
                    } else {
                        $('#ad-image-sold').remove();
                    };

                    $.Global.ImageWrapper.find('#City').empty();
                    $.Global.ImageWrapper.find('#City').append($.Global.Data[$.Global.CurrentIndex].City);
                    $.Global.ImageWrapper.find('#Price').empty();
                    if ($.Global.Data[$.Global.CurrentIndex].Price != "0") {
                        $.Global.ImageWrapper.find('#Price').append($.Global.Data[$.Global.CurrentIndex].Price);
                    } else {
                        $.Global.ImageWrapper.find('#Price').append($.Global.sold_label);
                    }
                    if (!isNaN($.Global.Data[$.Global.CurrentIndex].Price.replace(' ', '')) && $.Global.Data[$.Global.CurrentIndex].Price != "0") {
                        $.Global.ImageWrapper.find('#Price').append(' $');
                    }
                    $.Global.ImageWrapper.find('#Description').empty();
                    $.Global.ImageWrapper.find('#Description').append($.Global.Data[$.Global.CurrentIndex].Description);
                    $.Global.ImageWrapper.find('#Description').append($.Global.Data[$.Global.CurrentIndex].Description);

                    $.Global.ImageWrapper.find('#Description').append('<div id="ad-detail"></div>');
                    $('<a id="ad-detail-link" target="_blank"></a>').appendTo('#ad-detail');
                    $('#ad-detail-link').attr('href', $.Global.Data[$.Global.CurrentIndex].DetailURL);
                    $('#ad-detail-link').append($.Global.detail_label);

                    $.Global.ImageWrapper.find('#ad-image-wrapper').fadeTo($.Global.animation_speed, 1);

                    $('#ad-loading').css('display', 'none');

                    if ($.Global.autostart) {
                        $(this).moveAuto();
                    };
                };

                $.fn.movePrevious = function() {
                    $('#ad-loading').css('display', 'block');
                    window.clearInterval(oTimeout);
                    $.Global.CurrentIndex -= 1;
                    if ($.Global.CurrentIndex < 0) {
                        $.Global.CurrentIndex = $.Global.Data.length - 1;
                    }
                    $.Global.ImageWrapper.find('#ad-image-wrapper').fadeTo($.Global.animation_speed, 0, function() { $(this).renderCurrent(); });
                }

                $.fn.moveNext = function() {
                    if (!bIsOver) {
                        $('#ad-loading').css('display', 'block');
                        window.clearInterval(oTimeout);
                        $.Global.CurrentIndex += 1;
                        if ($.Global.CurrentIndex > $.Global.Data.length - 1) {
                            $.Global.CurrentIndex = 0;
                        }
                        $.Global.ImageWrapper.find('#ad-image-wrapper').fadeTo($.Global.animation_speed, 0, function() { $(this).renderCurrent(); });
                    }
                }

                $.fn.moveAuto = function() {
                    window.clearInterval(oTimeout);
                    oTimeout = setInterval("$(this).moveNext();", $.Global.speed);
                }
            };

            // *****************************
            // CLASSIC : STOP
            // *****************************

            // *****************************
            // VELVET : START
            // *****************************
            $.fn.Velvet = function(options) {
                $["Global"] = $.extend($.Global, options);

                if ($.Global.cssURL) {
                    $('<link rel="stylesheet" type="text/css" href="http://kaluxo.remax-quebec.com' + $.Global.cssURL + '">').appendTo('head');
                } else {
                    $('<link rel="stylesheet" type="text/css" href="' + $.Global.baseURL + '/css/velvet.css">').appendTo('head');
                }

                $.Global.MainWrapper = $(this);
                var sOrder = "";
                if ($.Global.order_by) {
                    sOrder = "&order=" + $.Global.order_by + "&dir=" + $.Global.order_dir;
                }
                var sOrder = "";
                if ($.Global.order_by) {
                    sOrder = "&order=" + $.Global.order_dir + "&dir=" + $.Global.order_by;
                }
                $.ajax({
                    type: "POST",
                    url: $.Global.alsURL + "?id=" + $.Global.id + sOrder + "&mod=json",
                    dataType: "jsonp",
                    timeout: 10000,
                    success: function(data) {
                        $.Global.Data = data
                        $(this).initControls($.Global.MainWrapper)
                        $(this).renderThumbs();
                        //renderCurrent($.Global.MainWrapper, $.Global.Data);
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                       // alert(thrownError);
                    }
                });

                $.fn.initControls = function() {
                    $('<table cellpadding="0" cellspacing="0" border="0">' +
                        '<tr><td>&nbsp;</td><td align="center"><div id="ad-wrapper-top"></div></td><td>&nbsp;</td></tr>' +
                        '<tr>' +
                            '<td align="right" id="ad-previous-wrapper"></td>' +
                            '<td align="center"><div id="ad-thumb-wrapper"></div></td>' +
                            '<td align="left" id="ad-next-wrapper"></td>' +
                        '</tr>' +
                      '</table>').appendTo($.Global.MainWrapper);

                    //        $('<div id="ad-wrapper-top"></div>' +
                    //            '<div id="ad-wrapper-bottom">' +
                    //                '<center><div id="ad-previous-wrapper"></div>' +
                    //                '<div id="ad-thumb-wrapper"></div>' +
                    //                '<div id="ad-next-wrapper"></div></center>' +
                    //            '</div>').appendTo($.Global.MainWrapper);

                    $.Global.ImageWrapper = $.Global.MainWrapper.find('#ad-wrapper-top');
                    $.Global.ImageWrapper.css('width', $.Global.image_width);

                    $.Global.ThumbWrapper = $.Global.MainWrapper.find('#ad-thumb-wrapper');
                    $.Global.ThumbWrapper.css('width', $.Global.image_width);
                    $('#ad-previous-wrapper').css('height', $.Global.thumb_height);
                    $('#ad-next-wrapper').css('height', $.Global.thumb_height);

                    $('<div id="ad-previous-image"></div>').appendTo('#ad-previous-wrapper');
                    $('<div id="ad-next-image"></div>').appendTo('#ad-next-wrapper');

                    $('<div id="ad-loading-wrapper"></div>').appendTo($.Global.ImageWrapper);
                    $('<div id="ad-loading"></div>').appendTo($('#ad-loading-wrapper'));
                    $('#ad-loading').css('width', $.Global.image_width);
                    $('#ad-loading').css('height', $.Global.image_height);

                    $('<div id="ad-image-wrapper"></div>').appendTo($.Global.ImageWrapper);
                    $('#ad-image-wrapper').css('width', $.Global.image_width);
                    $('#ad-image-wrapper').css('height', $.Global.image_height);
                    $('<a id="ad-image-link" target="_blank"></a>').appendTo('#ad-image-wrapper');
                    $('<img id="ad-image" border="0">').appendTo($('#ad-image-link'));
                    $('<div id="ad-info"></div>').appendTo('#ad-image-wrapper');
                    $('<div id="Price"></div>').appendTo('#ad-info');
                    $('<div id="Divider">-</div>').appendTo('#ad-info');
                    $('<div id="City"></div>').appendTo('#ad-info');

                    //        $('<div id="Title"></div>').appendTo($.Global.ImageWrapper);
                    //        $.Global.ImageWrapper.find('#Title').append($.Global.title_label);
                    //        $('<div id="City"></div>').appendTo($.Global.ImageWrapper);
                    //        $('<div id="Genre"></div>').appendTo($.Global.ImageWrapper);
                    //        $('<div id="Price"></div>').appendTo($.Global.ImageWrapper);

                    $('#ad-next-wrapper').find('#ad-next-image').click(
                        function() {
                            $(this).moveNext();
                            return false;
                        }
                    )
                    $('#ad-previous-wrapper').find('#ad-previous-image').click(
                        function() {
                            $(this).movePrevious();
                            return false;
                        }
                    )

                    $.Global.ImageWrapper.mouseover(
                        function() {
                            bIsOver = true;
                            return false;
                        }
                    );
                    $.Global.ImageWrapper.mouseout(
                        function() {
                            bIsOver = false;
                            return false;
                        }
                    );

                    $.Global.ImageWrapper.mouseover(
                        function() {
                            bIsOver = true;
                            return false;
                        }
                    );
                    $.Global.ImageWrapper.mouseout(
                        function() {
                            bIsOver = false;
                            return false;
                        }
                    );
                };

                $.fn.renderThumbs = function() {
                    $('<ul id="ad-thumbs-list"></ul>').appendTo($.Global.ThumbWrapper)
                    $('#ad-thumbs-list').css('width', ($.Global.image_width + $.Global.thumb_margin) * $.Global.Data.length);

                    $.each($.Global.Data, function(index, value) {
                        $('<li class="ad-thumbs-item" id="ad-thumbs-item' + index + '"></li>').appendTo('#ad-thumbs-list');
                        $('<img class="ad-thumbs" id="image' + index + '" />').appendTo('#ad-thumbs-item' + index);
                        $('#image' + index).attr('src', $.Global.Data[index].PictureURL.replace('stage', 'newthumbs'));
                        $('.ad-thumbs').css('width', $.Global.thumb_width);
                        $('.ad-thumbs').css('height', $.Global.thumb_height);
                        $('.ad-thumbs').css('margin', $.Global.thumb_margin);
                        $('#image' + index).fadeTo($.Global.animation_speed, 0.5);

                        $('#image' + index).click(function() {
                            $(this).moveTo(index);
                        });
                        $('#image' + index).mouseout(function() {
                            if (index != $.Global.CurrentIndex) {
                                $('#image' + index).fadeTo($.Global.animation_speed, 0.5);
                            };
                        });
                        $('#image' + index).mouseover(function() {
                            $('#image' + index).fadeTo($.Global.animation_speed, 1);
                        });
                    });
                    $('#image' + $.Global.CurrentIndex).fadeTo($.Global.animation_speed, 1, function() { $(this).renderCurrent() });
                };

                $.fn.renderCurrent = function() {
                    $('#ad-image-link').find('#ad-image').remove();
                    $('<img id="ad-image" border="0">').appendTo($('#ad-image-link'));

                    $('#ad-image-wrapper').find('#ad-image-link').attr('href', $.Global.Data[$.Global.CurrentIndex].DetailURL);

                    $('#ad-image-link').find('#ad-image').load(function() {
                        switch ($.Global.image_fit_to) {
                            case ('width'):
                                $('#ad-image-link').find('#ad-image').css('width', $.Global.image_width);
                                $('#ad-image-link').find('#ad-image').css('top', Number(($.Global.image_height - $(this).height()) / 2));
                                //alert(Number(($.Global.image_height - $(this).height()) / 2));
                                break;
                            case ('height'):
                                $('#ad-image-link').find('#ad-image').css('height', $.Global.image_height);
                                //$('#ad-image-link').find('#ad-image').css('left', Number(($.Global.image_width - $(this).width()) / 2));
                                break;
                            case ('none'):
                                break;
                            default:
                                $('#ad-image-link').find('#ad-image').css('width', $.Global.image_width);
                                $('#ad-image-link').find('#ad-image').css('height', $.Global.image_height);
                                break;
                        }
                        $('#ad-image-wrapper').css('overflow', 'hidden');
                    });

                    $('#ad-image-link').find('#ad-image').attr('src', $.Global.Data[$.Global.CurrentIndex].PictureURL);

                    if ($.Global.Data[$.Global.CurrentIndex].Price == "0") {
                        $('<div id="ad-image-sold"></div>').appendTo('#ad-image-wrapper');
                        $('#ad-image-sold').attr('width', $.Global.image_width);
                        $('#ad-image-sold').attr('height', $.Global.image_height);
                    } else {
                        $('#ad-image-wrapper').find('#ad-image-sold').remove();
                    };

                    $.Global.ImageWrapper.find('#City').empty();
                    $.Global.ImageWrapper.find('#City').append($.Global.Data[$.Global.CurrentIndex].City);
                    $.Global.ImageWrapper.find('#Genre').empty();
                    $.Global.ImageWrapper.find('#Genre').append($.Global.Data[$.Global.CurrentIndex].Genre);
                    $.Global.ImageWrapper.find('#Price').empty();
                    if ($.Global.Data[$.Global.CurrentIndex].Price != "0") {
                        $.Global.ImageWrapper.find('#Price').append($.Global.Data[$.Global.CurrentIndex].Price);
                    } else {
                        $.Global.ImageWrapper.find('#Price').append($.Global.sold_label);
                    }
                    if (!isNaN($.Global.Data[$.Global.CurrentIndex].Price.replace(' ', '')) && $.Global.Data[$.Global.CurrentIndex].Price != "0") {
                        $.Global.ImageWrapper.find('#Price').append(' $');
                    }
                    $.Global.ImageWrapper.find('#ad-detail').remove();
                    $('<div id="ad-detail"></div>').appendTo($.Global.ImageWrapper);
                    $('<a id="ad-detail-link" target="_blank"></a>').appendTo('#ad-detail');
                    $('#ad-detail-link').attr('href', $.Global.Data[$.Global.CurrentIndex].DetailURL);
                    $('#ad-detail-link').append($.Global.detail_label);

                    $.Global.ImageWrapper.find('#ad-image-wrapper').fadeTo($.Global.animation_speed, 1);

                    $('#ad-loading-wrapper').css('display', 'none');

                    if ($.Global.autostart) {
                        $(this).moveAuto();
                    };
                };


                $.fn.movePrevious = function() {
                    if (!bIsOver) {
                        $.Global.ImageWrapper.find('#ad-loading-wrapper').css('display', 'block');
                        window.clearInterval(oTimeout);
                        $('#image' + $.Global.CurrentIndex).fadeTo($.Global.animation_speed, 0.5);

                        $.Global.CurrentIndex -= 1;
                        if ($.Global.CurrentIndex < 0) {
                            $.Global.CurrentIndex = $.Global.Data.length - 1;
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-=" + ($.Global.thumb_width + ($.Global.thumb_margin * 3)) * $.Global.CurrentIndex + "px" }, $.Global.animation_speed);
                        } else {
                            //$.Global.ImageWrapperLeft.find('#ad-image-wrapper').fadeTo($.Global.animation_speed, 0, function() { renderCurrent(); });
                            //$.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-=" + $.Global.thumb_width + ($.Global.thumb_margin * 2) + "px" }, $.Global.animation_speed, function() { showNext(); });
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "+=" + ($.Global.thumb_width + ($.Global.thumb_margin * 3)) + "px" }, $.Global.animation_speed);
                        }
                        $('#image' + $.Global.CurrentIndex).fadeTo($.Global.animation_speed, 1, function() { $(this).renderCurrent() });
                    }
                }

                $.fn.moveNext = function() {
                    if (!bIsOver) {
                        $.Global.ImageWrapper.find('#ad-loading-wrapper').css('display', 'block');

                        window.clearInterval(oTimeout);
                        $('#image' + $.Global.CurrentIndex).fadeTo($.Global.animation_speed, 0.5);

                        $.Global.CurrentIndex += 1;
                        if ($.Global.CurrentIndex > $.Global.Data.length - 1) {
                            $.Global.CurrentIndex = 0;
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "0px" }, $.Global.animation_speed);
                        } else {
                            //$.Global.ImageWrapperLeft.find('#ad-image-wrapper').fadeTo($.Global.animation_speed, 0, function() { renderCurrent(); });
                            //$.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-=" + $.Global.thumb_width + ($.Global.thumb_margin * 2) + "px" }, $.Global.animation_speed, function() { showNext(); });
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-=" + ($.Global.thumb_width + ($.Global.thumb_margin * 3)) + "px" }, $.Global.animation_speed);
                        }
                        $('#image' + $.Global.CurrentIndex).fadeTo($.Global.animation_speed, 1, function() { $(this).renderCurrent() });
                    }
                }

                //    showPrevious = function() {
                //        //renderCurrent();
                //        //$.Global.ThumbWrapper.find('#ad-thumbs-list').css('right', $.Global.thumb_width);
                //        $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "right": "-=" + $.Global.thumb_width + ($.Global.thumb_margin * 2) + "px" }, $.Global.animation_speed);
                //    }
                //    showNext = function() {
                //        //renderCurrent();
                //        //$.Global.ThumbWrapper.find('#ad-thumbs-list').css('left', $.Global.thumb_width);
                //        $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-=" + $.Global.thumb_width + ($.Global.thumb_margin * 2) + "px" }, $.Global.animation_speed);
                //    }
                $.fn.moveTo = function(index) {
                    window.clearInterval(oTimeout);
                    $('#image' + $.Global.CurrentIndex).fadeTo($.Global.animation_speed, 0.5);

                    if (index > $.Global.CurrentIndex) {
                        $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-=" + (($.Global.thumb_width * (index - $.Global.CurrentIndex)) + ($.Global.thumb_margin * (index - $.Global.CurrentIndex) * 3)) + "px" }, $.Global.animation_speed);
                    } else if (index < $.Global.CurrentIndex) {
                        $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "+=" + (($.Global.thumb_width * (index - $.Global.CurrentIndex)) + ($.Global.thumb_margin * (index - $.Global.CurrentIndex) * 3)) + "px" }, $.Global.animation_speed);
                    }
                    $.Global.CurrentIndex = index;
                    $('#image' + $.Global.CurrentIndex).fadeTo($.Global.animation_speed, 1, function() { $(this).renderCurrent() });
                };

                $.fn.moveAuto = function() {
                    window.clearInterval(oTimeout);
                    oTimeout = setInterval("$(this).moveNext();", $.Global.speed);
                }
            };
            // *****************************
            // VELVET : STOP
            // *****************************

            // *****************************
            // DUO : START
            // *****************************
            $.fn.Duo = function(options) {
                $["Global"] = $.extend($.Global, options);

                if ($.Global.cssURL) {
                    $('<link rel="stylesheet" type="text/css" href="http://kaluxo.remax-quebec.com' + $.Global.cssURL + '">').appendTo('head');
                } else {
                    $('<link rel="stylesheet" type="text/css" href="' + $.Global.baseURL + '/css/duo.css">').appendTo('head');
                }

                $.Global.MainWrapper = $(this);

                $.ajax({
                    type: "POST",
                    url: $.Global.alsURL + "?id=" + $.Global.id + "&mod=json",
                    dataType: "jsonp",
                    timeout: 10000,
                    success: function(data) {
                        $.Global.Data = data
                        $(this).initControls($.Global.MainWrapper)
                        $(this).renderThumbs();
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError);
                    }
                });

                $.fn.initControls = function() {
                    $('<table cellpadding="0" cellspacing="0" border="0">' +
                        '<tr>' +
                            '<td align="center" style="width:' + $.Global.image_width + 'px"><div id="ad-thumb-wrapper"></div></td>' +
                            '<td align="left" id="ad-nav-wrapper"></td>' +
                        '</tr>' +
                      '</table>').appendTo($.Global.MainWrapper);

                    $.Global.ThumbWrapper = $.Global.MainWrapper.find('#ad-thumb-wrapper');
                    $.Global.ThumbWrapper.css('width', $.Global.image_width);

                    $('#ad-nav-wrapper').css('height', $.Global.thumb_height);
                    $('<div id="ad-previous-image"></div>').appendTo('#ad-nav-wrapper');
                    $('<div id="ad-next-image"></div>').appendTo('#ad-nav-wrapper');

                    $('#ad-nav-wrapper').find('#ad-next-image').click(
                        function() {
                            $(this).moveNext();
                            return false;
                        }
                    )

                    $('#ad-nav-wrapper').find('#ad-previous-image').click(
                        function() {
                            $(this).movePrevious();
                            return false;
                        }
                    )

                    $.Global.ThumbWrapper.mouseover(
                        function() {
                            bIsOver = true;
                            return false;
                        }
                    );
                    $.Global.ThumbWrapper.mouseout(
                        function() {
                            bIsOver = false;
                            return false;
                        }
                    );
                };

                $.fn.renderThumbs = function() {
                    $('<ul id="ad-thumbs-list"></ul>').appendTo($.Global.ThumbWrapper)
                    $('#ad-thumbs-list').css('width', ($.Global.image_width) * $.Global.Data.length);
                    $.each($.Global.Data, function(index, value) {
                        $('<li class="ad-thumbs-item" id="ad-thumbs-item' + index + '"></li>').appendTo('#ad-thumbs-list');
                        $('#ad-thumbs-item' + index).css('width', $.Global.thumb_width);
                        $('#ad-thumbs-item' + index).css('height', $.Global.thumb_height);
                        $('#ad-thumbs-item' + index).css('margin', $.Global.thumb_margin);

                        
                        $('<div class="ad-thumbs-item-wrapper" id="ad-thumbs-item-wrapper' + index + '"></div>').appendTo('#ad-thumbs-item' + index);
                        $('<a href="' + $.Global.Data[index].DetailURL + '" target="_blank" class="ad-thumbs-link" id="ad-thumbs-link' + index + '"></a>').appendTo('#ad-thumbs-item-wrapper' + index);
                        $('<img class="ad-thumbs" id="image' + index + '" />').appendTo('#ad-thumbs-link' + index);
                        //$('#image' + index).attr('src', $.Global.Data[index].PictureURL.replace('stage', 'newthumbs'));
                        $('#image' + index).attr('src', $.Global.Data[index].PictureURL);
                        
                        if ($.Global.Data[index].Price == "0") {
                            $('<div class="ad-image-sold" id="ad-image-sold' + index + '"></div>').appendTo('#ad-thumbs-link' + index);
                            $('#ad-image-sold' + index).css('width', $.Global.thumb_width);
                            $('#ad-image-sold' + index).css('height', $.Global.thumb_height);
                            $('<div class="ad-info-wrapper" id="ad-info-wrapper' + index + '"><div id="ad-price">' + $.Global.sold_label + '</div></div>').appendTo('#ad-thumbs-item-wrapper' + index);
                        } else {
                            $('#ad-thumbs-item-wrapper' + index).find('#ad-image-sold' + index).remove();
                            $('<div class="ad-info-wrapper" id="ad-info-wrapper' + index + '"><div id="ad-price">' + $.Global.Data[index].Price + '</div></div>').appendTo('#ad-thumbs-item-wrapper' + index);
                        };
                        
                        if (!isNaN($.Global.Data[index].Price.replace(' ', '')) && $.Global.Data[index].Price != "0") {
                            $('#ad-info-wrapper' + index).find('#ad-price').append(' $');
                        }
                        
                        $('#image' + index).click(function() {
                            $(this).moveTo(index);
                        });
                    });
                    $('.ad-thumbs').css('width', $.Global.thumb_width);
                    $('.ad-thumbs').css('height', $.Global.thumb_height);

                };

                $.fn.renderCurrent = function() {
                    if ($.Global.autostart) {
                        $(this).moveAuto();
                    };
                };

                $.fn.movePrevious = function() {
                    if (!bIsOver) {
                        window.clearInterval(oTimeout);

                        $.Global.CurrentIndex -= 2;
                        if ($.Global.CurrentIndex < 0) {
                            $.Global.CurrentIndex = ($.Global.Data.length - 2);
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-" + ($.Global.image_width) * Math.floor($.Global.CurrentIndex/2) + "px" }, $.Global.animation_speed);
                        } else {
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-" + ($.Global.image_width / 2 * $.Global.CurrentIndex) + "px" }, $.Global.animation_speed);
                        }
                    }
                }

                $.fn.moveNext = function() {
                
                    if (!bIsOver) {
                        window.clearInterval(oTimeout);

                        $.Global.CurrentIndex += 2;
                        if ($.Global.CurrentIndex > $.Global.Data.length - 1) {
                            $.Global.CurrentIndex = 0;
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "0px" }, $.Global.animation_speed);
                        } else {
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-" + ($.Global.image_width / 2 * $.Global.CurrentIndex) + "px" }, $.Global.animation_speed);
//                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-=" + $('#ad-thumb-wrapper').css('width') }, $.Global.animation_speed);
                        }
                    }
                }

                $.fn.moveAuto = function() {
                    window.clearInterval(oTimeout);
                    oTimeout = setInterval("$(this).moveNext();", $.Global.speed);
                }
            };

            // *****************************
            // DUO : STOP
            // *****************************

            // *****************************
            // THUMBS : START
            // *****************************
            $.fn.Thumbs = function(options) {
                $["Global"] = $.extend($.Global, options);

                if ($.Global.cssURL) {
                    $('<link rel="stylesheet" type="text/css" href="http://kaluxo.remax-quebec.com' + $.Global.cssURL + '">').appendTo('head');
                } else {
                    $('<link rel="stylesheet" type="text/css" href="' + $.Global.baseURL + '/css/thumbs.css">').appendTo('head');
                }

                $.Global.MainWrapper = $(this);

                $.ajax({
                    type: "POST",
                    url: $.Global.alsURL + "?id=" + $.Global.id + "&mod=json",
                    dataType: "jsonp",
                    timeout: 10000,
                    success: function(data) {
                        $.Global.Data = data
                        $(this).initControls($.Global.MainWrapper)
                        $(this).renderThumbs();
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError);
                    }
                });

                $.fn.initControls = function() {
                    $('#propertyFlipper').css('width','0px');

                    $('#propertyFlipper').animate({ "width": "100%" }, $.Global.animation_speed);
                    $('#propertyFlipper').css('height', $.Global.thumb_wrapper_height);

                    $('<table cellpadding="0" cellspacing="0" border="0" style="width:100%;height:100%;">' +
                        '<tr>' +
                            '<td align="center"><div id="ad-nav-wrapper-left"></div></td>' +
                            '<td align="center" style="width:' + (($.Global.thumb_width + ($.Global.thumb_margin*2)) * $.Global.thumb_nb_visible) + 'px"><div id="ad-thumb-wrapper"></div></td>' +
                            '<td align="center"><div id="ad-nav-wrapper-right"></div></td>' +
                        '</tr>' +
                      '</table>').appendTo($.Global.MainWrapper);

                    $.Global.ThumbWrapper = $.Global.MainWrapper.find('#ad-thumb-wrapper');
                    $.Global.ThumbWrapper.css('width', (($.Global.thumb_width + ($.Global.thumb_margin*2)) * $.Global.thumb_nb_visible));
                    $.Global.ThumbWrapper.css('height', $.Global.thumb_wrapper_height);
                    
                    $('<div id="ad-previous-image">&nbsp;</div>').appendTo('#ad-nav-wrapper-left');
                    $('<div id="ad-next-image">&nbsp;</div>').appendTo('#ad-nav-wrapper-right');

                    $('#ad-nav-wrapper-right').find('#ad-next-image').click(
                        function() {
                            $(this).moveNext();
                            return false;
                        }
                    )

                    $('#ad-nav-wrapper-left').find('#ad-previous-image').click(
                        function() {
                            $(this).movePrevious();
                            return false;
                        }
                    )

                    $.Global.ThumbWrapper.mouseover(
                        function() {
                            bIsOver = true;
                            return false;
                        }
                    );
                    $.Global.ThumbWrapper.mouseout(
                        function() {
                            bIsOver = false;
                            return false;
                        }
                    );
                };

                $.fn.renderThumbs = function() {
                    $('<ul id="ad-thumbs-list"></ul>').appendTo($.Global.ThumbWrapper)
                    //$('#ad-thumbs-list').css('width', ($.Global.image_width) * $.Global.Data.length);
                    $('#ad-thumbs-list').css('width', ((($.Global.thumb_width + ($.Global.thumb_margin*2)) * $.Global.thumb_nb_visible)) * $.Global.Data.length);
                    $('#ad-thumbs-list').css('height', $.Global.thumb_wrapper_height);

                    $.each($.Global.Data, function(index, value) {
                        $('<li class="ad-thumbs-item" id="ad-thumbs-item' + index + '"></li>').appendTo('#ad-thumbs-list');
                        $('#ad-thumbs-item' + index).css('width', $.Global.thumb_width);
                        $('#ad-thumbs-item' + index).css('height', $.Global.thumb_height);
                        
                        $('#ad-thumbs-item' + index).css('margin-left', $.Global.thumb_margin);
                        $('#ad-thumbs-item' + index).css('margin-right', $.Global.thumb_margin);
                        
                        $('<div class="ad-thumbs-item-wrapper" id="ad-thumbs-item-wrapper' + index + '"></div>').appendTo('#ad-thumbs-item' + index);
                        $('<a href="' + $.Global.Data[index].DetailURL + '" target="_blank" class="ad-thumbs-link" id="ad-thumbs-link' + index + '"></a>').appendTo('#ad-thumbs-item-wrapper' + index);
                        $('<img class="ad-thumbs" id="image' + index + '" />').appendTo('#ad-thumbs-link' + index);
                        //$('#image' + index).attr('src', $.Global.Data[index].PictureURL.replace('stage', 'newthumbs'));
                        $('#image' + index).attr('src', $.Global.Data[index].PictureURL);
                        
                        if ($.Global.Data[index].Price == "0") {
                            $('<div class="ad-image-sold" id="ad-image-sold' + index + '"></div>').appendTo('#ad-thumbs-link' + index);
                            $('#ad-image-sold' + index).css('width', $.Global.thumb_width);
                            $('#ad-image-sold' + index).css('height', $.Global.thumb_height);
                            $('<div class="ad-info-wrapper" id="ad-info-wrapper' + index + '"><div id="ad-price">' + $.Global.sold_label + '</div></div>').appendTo('#ad-thumbs-item-wrapper' + index);
                        } else {
                            $('#ad-thumbs-item-wrapper' + index).find('#ad-image-sold' + index).remove();
                            $('<div class="ad-info-wrapper" id="ad-info-wrapper' + index + '"><div id="ad-price">' + $.Global.Data[index].Price + '</div><div id="ad-city">' + $.Global.Data[index].City + '</div></div>').appendTo('#ad-thumbs-item-wrapper' + index);
                        };
                        
                        if (!isNaN($.Global.Data[index].Price.replace(' ', '')) && $.Global.Data[index].Price != "0") {
                            $('#ad-info-wrapper' + index).find('#ad-price').append(' $');
                        }
                        
                        $('#image' + index).click(function() {
                            $(this).moveTo(index);
                        });
                    });
                    $('.ad-thumbs').css('width', $.Global.thumb_width);
                    $('.ad-thumbs').css('height', $.Global.thumb_height);

                };

                $.fn.renderCurrent = function() {
                    if ($.Global.autostart) {
                        $(this).moveAuto();
                    };
                };

                $.fn.movePrevious = function() {
                    if (!bIsOver) {
                        window.clearInterval(oTimeout);

                        $.Global.CurrentIndex -= $.Global.thumb_nb_visible;
                        if ($.Global.CurrentIndex < 0) {
                            //$.Global.CurrentIndex = ($.Global.Data.length - Math.ceil($.Global.Data.length / $.Global.thumb_nb_visible));
                            //$.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-" + ($.Global.image_width) * Math.floor($.Global.CurrentIndex/2) + "px" }, $.Global.animation_speed);

                            var indexFirstThumbOfLastWindow = $.Global.Data.length - ($.Global.Data.length % $.Global.thumb_nb_visible);
                            $.Global.CurrentIndex = indexFirstThumbOfLastWindow;
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-" + ((($.Global.thumb_width + ($.Global.thumb_margin*2)) * $.Global.thumb_nb_visible)) * Math.ceil($.Global.CurrentIndex/$.Global.thumb_nb_visible) + "px" }, $.Global.animation_speed);
                        } else {
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-" + ((($.Global.thumb_width + ($.Global.thumb_margin*2)) * $.Global.thumb_nb_visible) / $.Global.thumb_nb_visible * $.Global.CurrentIndex) + "px" }, $.Global.animation_speed);
                        }
                    }
                }

                $.fn.moveNext = function() {
                
                    if (!bIsOver) {
                        window.clearInterval(oTimeout);

                        $.Global.CurrentIndex += $.Global.thumb_nb_visible;
                        if ($.Global.CurrentIndex > $.Global.Data.length - 1) {
                            $.Global.CurrentIndex = 0;
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "0px" }, $.Global.animation_speed);
                        } else {
                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-" + ((($.Global.thumb_width + ($.Global.thumb_margin*2)) * $.Global.thumb_nb_visible) / $.Global.thumb_nb_visible * $.Global.CurrentIndex) + "px" }, $.Global.animation_speed);
//                            $.Global.ThumbWrapper.find('#ad-thumbs-list').animate({ "left": "-=" + $('#ad-thumb-wrapper').css('width') }, $.Global.animation_speed);
                        }
                    }
                }

                $.fn.moveAuto = function() {
                    window.clearInterval(oTimeout);
                    oTimeout = setInterval("$(this).moveNext();", $.Global.speed);
                }
            };

            // *****************************
            // THUMBS : STOP
            // *****************************

        })(jQuery);

        function initFlipper() {

            if (typeof (loadFlipper) != 'undefined') {
                loadFlipper();
            }
        }
        initFlipper();
    }
}
initJQuery();
