viewmap = {

    map_el: [],
    show_map_el: [],
    hide_map_el: [],
    toggle_map_el: [],

    shown:0,

    init: function() {
        if (!$("viewmap_btn")) return false;
        viewmap.map_el=$(".map");
        viewmap.show_map_el=$(".showmap");
        viewmap.hide_map_el=$(".hidemap");
        viewmap.toggle_map_el=$(".togglemap");
        $("#viewmap_btn").css("background-image", "url(/images/view-map.gif)");
        viewmap.bindEvents();
    },
   
    bindEvents: function(ev) {
        viewmap.show_map_el.click(viewmap.showmap);
        viewmap.hide_map_el.click(viewmap.hidemap);
        viewmap.toggle_map_el.click(viewmap.togglemap);
        $("#viewmap_btn").mouseover(viewmap.mouseover).mouseout(viewmap.mouseout);
    },

    togglemap: function(ev) {
        with(viewmap) {
            (shown) ? hidemap() : showmap();
        }
    },

    showmap: function(ev) {
        $("#viewmap_btn").css("background-position", "0px -59px");
        $("#viewmap_btn_hide").css("display", 'inline');
        $("#viewmap_btn_show").css("display", 'none');
        viewmap.map_el.css("display","block");
        viewmap.shown=1;
    },

    hidemap: function(ev) {
        $("#viewmap_btn").css("background-position", "0px 0px");
        $("#viewmap_btn_show").css("display", 'inline');
        $("#viewmap_btn_hide").css("display", 'none');
        viewmap.map_el.css("display","none");
        viewmap.shown=0;
    },

    mouseover: function() {
        if (!viewmap.shown)
            $("#viewmap_btn").css("background-position", "0px -119px");
    },

    mouseout: function() {
        if (!viewmap.shown)
            $("#viewmap_btn").css("background-position", "0px 0px");
    }
};

$().ready(viewmap.init);
var sellercontactdetails = {

	details:[],
	button:0,

	logged:0,
	visible:0,
	advertid:0,

	init: function(advertid) {
                if (!$("#sellerdetailsbutton")) return false;
		sellercontactdetails.advertid=advertid;
                $("#sellerdetailsbutton").css({"overflow": "hidden", "background": "url(/images/contact-details.gif)"});
		sellercontactdetails.details = $(".contactdetails");
		if (!sellercontactdetails.details.length) return false;
		sellercontactdetails.hide();

                $("#sellerdetailsbutton")
                    .click(sellercontactdetails.toggle)
                    .hover(sellercontactdetails.mouseover, sellercontactdetails.mouseout);
	},
	
	toggle: function() {
            with(sellercontactdetails) {
                visible ? hide() : show();
            }
	},

	log: function() {
            if (!sellercontactdetails.logged) {
                $.get("/viewedcontactdetails.php?id=" + sellercontactdetails.advertid);	
                sellercontactdetails.logged=1;
            }
	},

        mouseover: function() {
            if (!sellercontactdetails.visible)
                $("sellerdetailsbutton").css("background-position", "0px 58px");
        },

        mouseout: function() {
            if (!sellercontactdetails.visible)
                $("sellerdetailsbutton").css("background-position", "0px 0px");
        },

	show: function() {
            sellercontactdetails.visible=1;
            $("sellerdetailsbutton").css("background-position", "0px -59px");
            $("sellerdetailsbutton_show").css("display", "none");
            $("sellerdetailsbutton_hide").css("display", "block");
            sellercontactdetails.log();
            sellercontactdetails.details.css("display", "block");
	},

	hide: function() {
            sellercontactdetails.visible=0;
            $("sellerdetailsbutton").css("background-position", "0px 0px");
            $("sellerdetailsbutton_show").css("display", "block");
            $("sellerdetailsbutton_hide").css("display", "none");
            sellercontactdetails.details.css("display", "none");
	}
};

(function() {
var advertview = {
	
    last:[],

    init: function() {
        $(".contactbutton[box]")
            .css("cursor", "pointer")
            .click(advertview.open);

        advertview.close();
        $(".contactdialog").click(function(ev) {ev.stopPropagation();});
        $(document).click(advertview.lostfocus);
    },

    open: function(ev) {
        var button = $(this);
        var box = $("#" + button.attr("box"));
        advertview.close();
        if (box) {
            if (advertview.last == button.attr("id")) { 
                advertview.last = "";
            } else {
                box.css("display", "block");
                button.addClass("selected");
                advertview.last = button.attr("id");
            } 
        }
        $("input", box)[0].focus();
        return false;
    },

    lostfocus: function(ev) {
        advertview.close();
        advertview.last="";
        ev.stopPropagation();
    },

    close: function() {
        $(".contactdialog").css("display", "none");
        $(".contactbutton.selected").removeClass("selected");
    }
}
$().ready(advertview.init);
}) ();

var TINY= {};

/*
function $(i) {
    return document.getElementById(i)
}
*/

function $$(e,p) {
    p=p||document;
    return p.getElementsByTagName(e)
}

TINY.slideshow=function(n) {
    this.infoSpeed=this.imgSpeed=this.speed=10;
    this.thumbOpacity=this.navHover=70;
    this.navOpacity=25;
    this.scrollSpeed=5;
    this.letterbox='#fff';
    this.n=n;
    this.c=0;
    this.a=[];
};

TINY.slideshow.prototype= {

    init: function(s,z,b,f,q) {
	s=$(s);
        var m=$$('li',s),i=0,w=0;
        this.running=false;
        this.l=m.length;
        this.q=$(q);
        this.f=$(z);
        this.r=$(this.info);
        this.o=parseInt(TINY.style.val(z,'width'));
        if (this.thumbs) {
            var u=$(this.left),r=$(this.right);
            u.onmouseover=new Function('TINY.scroll.init("'+this.thumbs+'",-1,'+this.scrollSpeed+')');
            u.onmouseout=r.onmouseout=new Function('TINY.scroll.cl("'+this.thumbs+'")');
            r.onmouseover=new Function('TINY.scroll.init("'+this.thumbs+'",1,'+this.scrollSpeed+')');
            this.p=$(this.thumbs)
        }

        for (i; i<this.l; i++) {
            this.a[i]= {};
            var h=m[i],a=this.a[i];
            a.t=$$('h3',h)[0].innerHTML;
            a.d=$$('p',h)[0].innerHTML;
            a.l=$$('a',h)[0]?$$('a',h)[0].href:'';
            a.p=$$('span',h)[0].innerHTML;
            if (this.thumbs) {
                    var g=$$('img',h)[0];
                    this.p.appendChild(g);
                    w+=parseInt(g.offsetWidth);
                    if (i!=this.l-1) {
                        g.style.marginRight=this.spacing+'px';
                        w+=this.spacing
                    }
                    this.p.style.width=w+'px';
                    g.style.opacity=this.thumbOpacity/100;
                    g.style.filter='alpha(opacity='+this.thumbOpacity+')';
                    g.onmouseover=new Function('TINY.alpha.set(this,100,5)');
                    g.onmouseout=new Function('TINY.alpha.set(this,'+this.thumbOpacity+',5)');
                    g.onclick=new Function(this.n+'.pr('+i+',1)');
            }
        }

        if (b&&f) {
            b=$(b);
            f=$(f);
            b.style.opacity=f.style.opacity=this.navOpacity/100;
            b.style.filter=f.style.filter='alpha(opacity='+this.navOpacity+')';
            b.onmouseover=f.onmouseover=new Function('TINY.alpha.set(this,'+this.navHover+',5)');
            b.onmouseout=f.onmouseout=new Function('TINY.alpha.set(this,'+this.navOpacity+',5)');
            b.onclick=new Function(this.n+'.mv(-1,1)');
            f.onclick=new Function(this.n+'.mv(1,1)');
        }

        if (this.auto) {
            this.is(0, 0);
            this.running=true;
            //$("startstop").src="/images/slideshow/pause.png";
        } else {
            this.is(0, 1);
            this.running=false;
            //$("startstop").src="/images/slideshow/play.png";
        }
    },

    toggle: function() {
        if (!this.running) {
            this.is(0, 0);
            this.running=true;
            //$("startstop").src="/images/slideshow/pause.png";
        } else {
            if (this.lt) clearTimeout(this.lt);
            if (this.at) clearTimeout(this.at);
            this.running=false;
            //$("startstop").src="/images/slideshow/play.png";
        }
    },

    stop_all: function() {
        if (this.lt) clearTimeout(this.lt);
        if (this.at) clearTimeout(this.at);
    },

    mv: function(d,c) {
        var t=this.c+d;
        this.c=t=t<0?this.l-1:t>this.l-1?0:t;
        this.pr(t,c)
    },

    pr: function(t,c) {
            clearTimeout(this.lt);
            if (c) {
                clearTimeout(this.at);
            }
            this.c=t;
            this.is(t,c);
    },

    is: function(s,c) {
        if(this.info) {
            TINY.height.set(this.r,1,this.infoSpeed/2,-1); 
        }

        var i=new Image();
        i.style.opacity=0;
        i.style.filter='alpha(opacity=0)';
        this.i=i;
        i.onload=new Function(this.n+'.le('+s+','+c+')');
        i.src=this.a[s].p;
        if(this.thumbs) {
            var a=$$('img',this.p),l=a.length,x=0;
            for(x; x<l; x++) {
                a[x].style.borderColor=x!=s?'':this.active
            }
        }

    },

    le: function(s,c) {
        this.f.appendChild(this.i);
        var w=this.o-parseInt(this.i.offsetWidth);
        if (w>0) {
            var l=Math.floor(w/2);
            this.i.style.borderLeft=l+'px solid '+this.letterbox;
            this.i.style.borderRight=(w-l)+'px solid '+this.letterbox
        }

        TINY.alpha.set(this.i,100,this.imgSpeed);
        var n=new Function(this.n+'.nf('+s+')');
        this.lt=setTimeout(n,this.imgSpeed*100);
        if (!c) {
            this.at=setTimeout(new Function(this.n+'.mv(1,0)'),this.speed*1000);
        }

        if (this.a[s].l!='') {
            this.q.onclick=new Function('window.location="'+this.a[s].l+'"');
            this.q.onmouseover=new Function('this.className="'+this.link+'"');
            this.q.onmouseout=new Function('this.className=""');
            this.q.style.cursor='pointer';
        } else {
            this.q.onclick=this.q.onmouseover=null;
            this.q.style.cursor='default';
        }

        var m=$$('img',this.f);
        //if (m.length>2) {
        if (m.length>1) {
            this.f.removeChild(m[0]);
        }
    }, 

    nf: function(s) {
        if(this.info) {
            s=this.a[s];
            $$('h3',this.r)[0].innerHTML=s.t;
            $$('p',this.r)[0].innerHTML=s.d;
            this.r.style.height='auto';
            var h=parseInt(this.r.offsetHeight);
            this.r.style.height=0;
            TINY.height.set(this.r,h,this.infoSpeed,0);
        }
    }
};

TINY.scroll=function() {
    return {
	init: function(e,d,s) {
            e=typeof e=='object'?e:$(e);
            var p=e.style.left||TINY.style.val(e,'left');
            e.style.left=p;
            var l=d==1?parseInt(e.offsetWidth)-parseInt(e.parentNode.offsetWidth):0;
            e.si=setInterval(function() { TINY.scroll.mv(e,l,d,s) }, 20);
        }, 
    
        mv:function(e,l,d,s) {
            var c=parseInt(e.style.left);
            if (c==l) {
                TINY.scroll.cl(e);
            } else {
                var i=Math.abs(l+c);
                i=i<s?i:s;
                var n=c-i*d;
                e.style.left=n+'px';
            }
        },
    
        cl:function(e) {
            e=typeof e=='object'?e:$(e);
            clearInterval(e.si);
        }
    }
}();

TINY.height=function() {
    return {
        set: function(e,h,s,d) {
            e=typeof e=='object'?e:$(e);
            var oh=e.offsetHeight,ho=e.style.height||TINY.style.val(e,'height');
            ho=oh-parseInt(ho);
            var hd=oh-ho>h?-1:1;
            clearInterval(e.si);
            e.si=setInterval(function() { TINY.height.tw(e,h,ho,hd,s) } ,20);
        }, 
    
        tw: function(e,h,ho,hd,s) {
            var oh=e.offsetHeight-ho;
            if(oh==h) {
                clearInterval(e.si)
            } else {
                if (oh!=h) {
                    var foo =oh+(Math.ceil(Math.abs(h-oh)/s)*hd);
                    //console.log("assigning: " + foo);
                    if (foo >= 0) e.style.height = foo + "px";
                }
            }
        }
    }
}();

TINY.alpha=function() {
    return {
	set:function(e,a,s) {
            e=typeof e=='object'?e:$(e);
            var o=e.style.opacity||TINY.style.val(e,'opacity'),d=a>o*100?1:-1;
            e.style.opacity=o;
            clearInterval(e.ai);
            e.ai=setInterval(function() { TINY.alpha.tw(e,a,d,s) } ,20);
        }, 
        
        tw:function(e,a,d,s) {
            var o=Math.round(e.style.opacity*100);
            if(o==a) {
                clearInterval(e.ai)
            } else {
                var n=o+Math.ceil(Math.abs(a-o)/s)*d;
                e.style.opacity=n/100;
                e.style.filter='alpha(opacity='+n+')';
            }
        }
    }
}();

TINY.style=function() {
    return {
        val:function(e,p) {
            e=typeof e=='object'?e:$(e);
            return e.currentStyle?e.currentStyle[p]:document.defaultView.getComputedStyle(e,null).getPropertyValue(p)
        }
    }
}();
/**
 * Copyright (c) 2009 Anders Ekdahl (http://coffeescripter.com/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version: 1.2.2
 *
 * Demo and documentation: http://coffeescripter.com/code/ad-gallery/
 */
(function($) {
  $.fn.adGallery = function(options) {
    var defaults = { loader_image: '/images/loading.gif',
                     start_at_index: 0,
                     thumb_opacity: 0.7,
                     animate_first_image: false,
                     animation_speed: 400,
                     width: false,
                     height: false,
                     display_next_and_prev: true,
                     display_back_and_forward: true,
                     scroll_jump: 0, // If 0, it jumps the width of the container
                     slideshow: {
                       enable: true,
                       autostart: false,
                       speed: 3000,
                       start_label: 'Start Slideshow',
                       stop_label: 'Stop Slideshow',
                       stop_on_scroll: true,
                       countdown_prefix: '(',
                       countdown_sufix: ')',
                       onStart: false,
                       onStop: false
                     },
                     effect: 'slide-hori', // or 'slide-vert', 'fade', or 'resize', 'none'
                     enable_keyboard_move: true,
                     cycle: true,
                     callbacks: {
                       init: false,
                       afterImageVisible: false,
                       beforeImageVisible: false
                     }
    };
    var settings = $.extend(false, defaults, options);
    if(options && options.slideshow) {
      settings.slideshow = $.extend(false, defaults.slideshow, options.slideshow);
    };
    if(!settings.slideshow.enable) {
      settings.slideshow.autostart = false;
    };
    var galleries = [];
    $(this).each(function() {
      var gallery = new AdGallery(this, settings);
      galleries[galleries.length] = gallery;
    });
    // Sorry, breaking the jQuery chain because the gallery instances
    // are returned so you can fiddle with them
    return galleries;
  };

  function VerticalSlideAnimation(img_container, direction, desc) {
    var current_top = parseInt(img_container.css('top'), 10);
    if(direction == 'left') {
      var old_image_top = '-'+ this.image_wrapper_height +'px';
      img_container.css('top', this.image_wrapper_height +'px');
    } else {
      var old_image_top = this.image_wrapper_height +'px';
      img_container.css('top', '-'+ this.image_wrapper_height +'px');
    };
    if(desc) {
      desc.css('bottom', '-'+ desc[0].offsetHeight +'px');
      desc.animate({bottom: 0}, this.settings.animation_speed * 2);
    };
    return {old_image: {top: old_image_top},
            new_image: {top: current_top}};
  };

  function HorizontalSlideAnimation(img_container, direction, desc) {
    var current_left = parseInt(img_container.css('left'), 10);
    if(direction == 'left') {
      var old_image_left = '-'+ this.image_wrapper_width +'px';
      img_container.css('left',this.image_wrapper_width +'px');
    } else {
      var old_image_left = this.image_wrapper_width +'px';
      img_container.css('left','-'+ this.image_wrapper_width +'px');
    };
    if(desc) {
      desc.css('bottom', '-'+ desc[0].offsetHeight +'px');
      desc.animate({bottom: 0}, this.settings.animation_speed * 2);
    };
    return {old_image: {left: old_image_left},
            new_image: {left: current_left}};
  };

  function ResizeAnimation(img_container, direction, desc) {
    var image_width = img_container.width();
    var image_height = img_container.height();
    var current_left = parseInt(img_container.css('left'), 10);
    var current_top = parseInt(img_container.css('top'), 10);
    img_container.css({width: 0, height: 0, top: this.image_wrapper_height / 2, left: this.image_wrapper_width / 2});
    return {old_image: {width: 0,
                        height: 0,
                        top: this.image_wrapper_height / 2,
                        left: this.image_wrapper_width / 2},
            new_image: {width: image_width,
                        height: image_height,
                        top: current_top,
                        left: current_left}};
  };

  function FadeAnimation(img_container, direction, desc) {
    img_container.css('opacity', 0);
    return {old_image: {opacity: 0},
            new_image: {opacity: 1}};
  };

  // Sort of a hack, will clean this up... eventually
  function NoneAnimation(img_container, direction, desc) {
    img_container.css('opacity', 0);
    return {old_image: {opacity: 0},
            new_image: {opacity: 1},
            speed: 0};
  };

  function AdGallery(wrapper, settings) {
    this.init(wrapper, settings);
  };
  AdGallery.prototype = {
    // Elements
    wrapper: false,
    image_wrapper: false,
    gallery_info: false,
    nav: false,
    loader: false,
    preloads: false,
    thumbs_wrapper: false,
    scroll_back: false,
    scroll_forward: false,
    next_link: false,
    prev_link: false,

    slideshow: false,
    image_wrapper_width: 0,
    image_wrapper_height: 0,
    current_index: 0,
    current_image: false,
    nav_display_width: 0,
    settings: false,
    images: false,
    in_transition: false,
    animations: false,
    init: function(wrapper, settings) {
      var context = this;
      this.wrapper = $(wrapper);
      this.settings = settings;
      this.setupElements();
      this.setupAnimations();
      if(this.settings.width) {
        this.image_wrapper_width = this.settings.width;
        this.image_wrapper.width(this.settings.width);
        this.wrapper.width(this.settings.width);
      } else {
        this.image_wrapper_width = this.image_wrapper.width();
      };
      if(this.settings.height) {
        this.image_wrapper_height = this.settings.height;
        this.image_wrapper.height(this.settings.height);
      } else {
        this.image_wrapper_height = this.image_wrapper.height();
      };
      this.nav_display_width = this.nav.width();
      this.current_index = 0;
      this.current_image = false;
      this.in_transition = false;
      this.findImages();
      if(this.settings.display_next_and_prev) {
        this.initNextAndPrev();
      };
      // The slideshow needs a callback to trigger the next image to be shown
      // but we don't want to give it access to the whole gallery instance
      var nextimage_callback = function(callback) {
        return context.nextImage(callback);
      };
      this.slideshow = new AdGallerySlideshow(nextimage_callback, this.settings.slideshow);
      this.controls.append(this.slideshow.create());
      if(this.settings.slideshow.enable) {
        this.slideshow.enable();
      } else {
        this.slideshow.disable();
      };
      if(this.settings.display_back_and_forward) {
        this.initBackAndForward();
      };
      if(this.settings.enable_keyboard_move) {
        this.initKeyEvents();
      };
      var start_at = this.settings.start_at_index;
      if(window.location.hash && window.location.hash.indexOf('#ad-image') === 0) {
        start_at = window.location.hash.replace(/[^0-9]+/g, '');
        // Check if it's a number
        if((start_at * 1) != start_at) {
          start_at = this.settings.start_at_index;
        };
      };

      this.loading(true);
      this.showImage(start_at,
        function() {
          // We don't want to start the slideshow before the image has been
          // displayed
          if(context.settings.slideshow.autostart) {
            context.preloadImage(start_at + 1);
            context.slideshow.start();
          };
        }
      );
      this.fireCallback(this.settings.callbacks.init);
    },
    setupAnimations: function() {
      this.animations = {
        'slide-vert': VerticalSlideAnimation,
        'slide-hori': HorizontalSlideAnimation,
        'resize': ResizeAnimation,
        'fade': FadeAnimation,
        'none': NoneAnimation
      };
    },
    setupElements: function() {
      this.controls = this.wrapper.find('.ad-controls');
      this.gallery_info = $('<p class="ad-info"></p>');
      this.controls.append(this.gallery_info);
      this.image_wrapper = this.wrapper.find('.ad-image-wrapper');
      this.image_wrapper.empty();
      this.nav = this.wrapper.find('.ad-nav');
      this.thumbs_wrapper = this.nav.find('.ad-thumbs');
      this.preloads = $('<div class="ad-preloads"></div>');
      this.loader = $('<img class="ad-loader" src="'+ this.settings.loader_image +'">');
      this.image_wrapper.append(this.loader);
      this.loader.hide();
      $(document.body).append(this.preloads);
    },
    loading: function(bool) {
      if(bool) {
        this.loader.show();
      } else {
        this.loader.hide();
      };
    },
    addAnimation: function(name, fn) {
      if($.isFunction(fn)) {
        this.animations[name] = fn;
      };
    },
    findImages: function() {
      var context = this;
      this.images = [];
      var thumb_wrapper_width = 0;
      var thumbs_loaded = 0;
      var thumbs = this.thumbs_wrapper.find('a');
      var thumb_count = thumbs.length;
      if(this.settings.thumb_opacity < 1) {
        thumbs.find('img').css('opacity', this.settings.thumb_opacity);
      };
      thumbs.each(
        function(i) {
          var link = $(this);
          var image_src = link.attr('href');
          var thumb = link.find('img');
          // Check if the thumb has already loaded
          if(!context.isImageLoaded(thumb[0])) {
            thumb.load(
              function() {
                thumb_wrapper_width += this.parentNode.parentNode.offsetWidth;
                thumbs_loaded++;
              }
            );
          } else{
            thumb_wrapper_width += thumb[0].parentNode.parentNode.offsetWidth;
            thumbs_loaded++;
          };
          link.addClass('ad-thumb'+ i);
          link.click(
            function() {
              context.showImage(i);
              context.slideshow.stop();
              return false;
            }
          ).hover(
            function() {
              if(!$(this).is('.ad-active') && context.settings.thumb_opacity < 1) {
                $(this).find('img').fadeTo(300, 1);
              };
              context.preloadImage(i);
            },
            function() {
              if(!$(this).is('.ad-active') && context.settings.thumb_opacity < 1) {
                $(this).find('img').fadeTo(300, context.settings.thumb_opacity);
              };
            }
          );
          var desc = false;
          if(thumb.data('ad-desc')) {
            desc = thumb.data('ad-desc');
          } else if(thumb.attr('longdesc') && thumb.attr('longdesc').length) {
            desc = thumb.attr('longdesc');
          };
          var title = false;
          if(thumb.data('ad-title')) {
            title = thumb.data('ad-title');
          } else if(thumb.attr('title') && thumb.attr('title').length) {
            title = thumb.attr('title');
          };
          context.images[i] = { thumb: thumb.attr('src'), image: image_src, error: false,
                                preloaded: false, desc: desc, title: title, size: false };
        }
      );
      // Wait until all thumbs are loaded, and then set the width of the ul
      var inter = setInterval(
        function() {
          if(thumb_count == thumbs_loaded) {
            context.nav.find('.ad-thumb-list').css('width', thumb_wrapper_width +'px');
            clearInterval(inter);
          };
        },
        100
      );
    },
    initKeyEvents: function() {
      var context = this;
      $(document).keydown(
        function(e) {
          if(e.keyCode == 39) {
            // right arrow
            context.nextImage();
            context.slideshow.stop();
          } else if(e.keyCode == 37) {
            // left arrow
            context.prevImage();
            context.slideshow.stop();
          };
        }
      );
    },
    initNextAndPrev: function() {
      this.next_link = $('<div class="ad-next"><div class="ad-next-image"></div></div>');
      this.prev_link = $('<div class="ad-prev"><div class="ad-prev-image"></div></div>');
      this.image_wrapper.append(this.next_link);
      this.image_wrapper.append(this.prev_link);
      var context = this;
      this.prev_link.add(this.next_link).mouseover(
        function(e) {
          // IE 6 hides the wrapper div, so we have to set it's width
          $(this).css('height', context.image_wrapper_height);
          $(this).find('div').show();
        }
      ).mouseout(
        function(e) {
          $(this).find('div').hide();
        }
      ).click(
        function() {
          if($(this).is('.ad-next')) {
            context.nextImage();
            context.slideshow.stop();
          } else {
            context.prevImage();
            context.slideshow.stop();
          };
        }
      ).find('div').css('opacity', 0.7);
    },
    initBackAndForward: function() {
      var context = this;
      this.scroll_forward = $('<div class="ad-forward"></div>');
      this.scroll_back = $('<div class="ad-back"></div>');
      this.nav.append(this.scroll_forward);
      this.nav.prepend(this.scroll_back);
      var has_scrolled = 0;
      var thumbs_scroll_interval = false;
      $(this.scroll_back).add(this.scroll_forward).click(
        function() {
          // We don't want to jump the whole width, since an image
          // might be cut at the edge
          var width = context.nav_display_width - 50;
          if(context.settings.scroll_jump > 0) {
            var width = context.settings.scroll_jump;
          };
          if($(this).is('.ad-forward')) {
            var left = context.thumbs_wrapper.scrollLeft() + width;
          } else {
            var left = context.thumbs_wrapper.scrollLeft() - width;
          };
          if(context.settings.slideshow.stop_on_scroll) {
            context.slideshow.stop();
          };
          context.thumbs_wrapper.animate({scrollLeft: left +'px'});
          return false;
        }
      ).css('opacity', 0.6).hover(
        function() {
          var direction = 'left';
          if($(this).is('.ad-forward')) {
            direction = 'right';
          };
          thumbs_scroll_interval = setInterval(
            function() {
              has_scrolled++;
              // Don't want to stop the slideshow just because we scrolled a pixel or two
              if(has_scrolled > 30 && context.settings.slideshow.stop_on_scroll) {
                context.slideshow.stop();
              };
              var left = context.thumbs_wrapper.scrollLeft() + 1;
              if(direction == 'left') {
                left = context.thumbs_wrapper.scrollLeft() - 1;
              };
              context.thumbs_wrapper.scrollLeft(left);
            },
            10
          );
          $(this).css('opacity', 1);
        },
        function() {
          has_scrolled = 0;
          clearInterval(thumbs_scroll_interval);
          $(this).css('opacity', 0.6);
        }
      );
    },
    _afterShow: function() {
      this.gallery_info.html((this.current_index + 1) +' / '+ this.images.length);
      if(!this.settings.cycle) {
        // Needed for IE
        this.prev_link.show().css('height', this.image_wrapper_height);
        this.next_link.show().css('height', this.image_wrapper_height);
        if(this.current_index == (this.images.length - 1)) {
          this.next_link.hide();
        };
        if(this.current_index == 0) {
          this.prev_link.hide();
        };
      };
      this.fireCallback(this.settings.callbacks.afterImageVisible);
    },
    /**
     * Checks if the image is small enough to fit inside the container
     * If it's not, shrink it proportionally
     */
    _getContainedImageSize: function(image_width, image_height) {
      if(image_height > this.image_wrapper_height) {
        var ratio = image_width / image_height;
        image_height = this.image_wrapper_height;
        image_width = this.image_wrapper_height * ratio;
      };
      if(image_width > this.image_wrapper_width) {
  	    var ratio = image_height / image_width;
  	    image_width = this.image_wrapper_width;
  	    image_height = this.image_wrapper_width * ratio;
  	  };
      return {width: image_width, height: image_height};
    },
    /**
     * If the image dimensions are smaller than the wrapper, we position
     * it in the middle anyway
     */
    _centerImage: function(img_container, image_width, image_height) {
      img_container.css('top', '0px');
      if(image_height < this.image_wrapper_height) {
        var dif = this.image_wrapper_height - image_height;
        img_container.css('top', (dif / 2) +'px');
      };
      img_container.css('left', '0px');
      if(image_width < this.image_wrapper_width) {
        var dif = this.image_wrapper_width - image_width;
        img_container.css('left', (dif / 2) +'px');
      };
    },
    _getDescription: function(image) {
      var desc = false;
      if(image.desc.length || image.title.length) {
        var title = '';
        if(image.title.length) {
          title = '<strong class="ad-description-title">'+ image.title +'</strong>';
        };
        var desc = '';
        if(image.desc.length) {
          desc = '<span>'+ image.desc +'</span>';
        };
        desc = $('<p class="ad-image-description">'+ title + desc +'</p>');
      };
      return desc;
    },
    /**
     * @param function callback Gets fired when the image has loaded, is displaying
     *                          and it's animation has finished
     */
    showImage: function(index, callback) {
      if(this.images[index] && !this.in_transition) {
        var context = this;
        var image = this.images[index];
        this.in_transition = true;
        if(!image.preloaded) {
          this.loading(true);
          this.preloadImage(index, function() {
            context.loading(false);
            context._showWhenLoaded(index, callback);
          });
        } else {
          this._showWhenLoaded(index, callback);
        };
      };
    },
    /**
     * @param function callback Gets fired when the image has loaded, is displaying
     *                          and it's animation has finished
     */
    _showWhenLoaded: function(index, callback) {
      if(this.images[index]) {
        var context = this;
        var image = this.images[index];
        var img_container = $(document.createElement('div')).addClass('ad-image');
        var img = $(new Image()).attr('src', image.image);
        img_container.append(img);
        this.image_wrapper.prepend(img_container);
        var size = this._getContainedImageSize(image.size.width, image.size.height);
        img.attr('width', size.width);
        img.attr('height', size.height);
        img_container.css({width: size.width +'px', height: size.height +'px'});
        this._centerImage(img_container, size.width, size.height);
        var desc = this._getDescription(image, img_container);
        if(desc) {
          img_container.append(desc);
          var width = size.width - parseInt(desc.css('padding-left'), 10) - parseInt(desc.css('padding-right'), 10);
          desc.css('width', width +'px');
        };
        this.highLightThumb(this.nav.find('.ad-thumb'+ index));

        var direction = 'right';
        if(this.current_index < index) {
          direction = 'left';
        };
        this.fireCallback(this.settings.callbacks.beforeImageVisible);
        if(this.current_image || this.settings.animate_first_image) {
          var animation_speed = this.settings.animation_speed;
          var easing = 'swing';
          var animation = this.animations[this.settings.effect].call(this, img_container, direction, desc);
          if(typeof animation.speed != 'undefined') {
            animation_speed = animation.speed;
          };
          if(typeof animation.easing != 'undefined') {
            easing = animation.easing;
          };
          if(this.current_image) {
            var old_image = this.current_image;
            old_image.animate(animation.old_image, animation_speed, easing,
              function() {
                old_image.remove();
              }
            );
          };
          img_container.animate(animation.new_image, animation_speed, easing,
            function() {
              context.current_index = index;
              context.current_image = img_container;
              context.in_transition = false;
              context._afterShow();
              context.fireCallback(callback);
            }
          );
        } else {
          this.current_index = index;
          this.current_image = img_container;
          this.in_transition = false;
          context._afterShow();
          this.fireCallback(callback);
        };
      };
    },
    nextIndex: function() {
      if(this.current_index == (this.images.length - 1)) {
        if(!this.settings.cycle) {
          return false;
        };
        var next = 0;
      } else {
        var next = this.current_index + 1;
      };
      return next;
    },
    nextImage: function(callback) {
      var next = this.nextIndex();
      if(next === false) return false;
      this.preloadImage(next + 1);
      this.showImage(next, callback);
      return true;
    },
    prevIndex: function() {
      if(this.current_index == 0) {
        if(!this.settings.cycle) {
          return false;
        };
        var prev = this.images.length - 1;
      } else {
        var prev = this.current_index - 1;
      };
      return prev;
    },
    prevImage: function(callback) {
      var prev = this.prevIndex();
      if(prev === false) return false;
      this.preloadImage(prev - 1);
      this.showImage(prev, callback);
      return true;
    },
    preloadAll: function() {
      var context = this;
      var i = 0;
      function preloadNext() {
        if(i < context.images.length) {
          i++;
          context.preloadImage(i, preloadNext);
        };
      };
      context.preloadImage(i, preloadNext);
    },
    preloadImage: function(index, callback) {
      if(this.images[index]) {
        var image = this.images[index];
        if(!this.images[index].preloaded) {
          var img = $(new Image());
          img.attr('src', image.image);
          if(!this.isImageLoaded(img[0])) {
            this.preloads.append(img);
            var context = this;
            img.load(
              function() {
                image.preloaded = true;
                image.size = { width: this.width, height: this.height };
                context.fireCallback(callback);
              }
            ).error(
              function() {
                image.error = true;
                image.preloaded = false;
                image.size = false;
              }
            );
          } else {
            image.preloaded = true;
            image.size = { width: img[0].width, height: img[0].height };
            this.fireCallback(callback);
          };
        } else {
          this.fireCallback(callback);
        };
      };
    },
    isImageLoaded: function(img) {
      if(typeof img.complete != 'undefined' && !img.complete) {
        return false;
      };
      if(typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) {
        return false;
      };
      return true;
    },
    highLightThumb: function(thumb) {
      this.thumbs_wrapper.find('.ad-active').removeClass('ad-active');
      thumb.addClass('ad-active');
      if(this.settings.thumb_opacity < 1) {
        this.thumbs_wrapper.find('a:not(.ad-active) img').fadeTo(300, this.settings.thumb_opacity);
        thumb.find('img').fadeTo(300, 1);
      };
      var left = thumb[0].parentNode.offsetLeft;
      left -= (this.nav_display_width / 2) - (thumb[0].offsetWidth / 2);
      this.thumbs_wrapper.animate({scrollLeft: left +'px'});
    },
    fireCallback: function(fn) {
      if($.isFunction(fn)) {
        fn.call(this);
      };
    }
  };

  function AdGallerySlideshow(nextimage_callback, settings) {
    this.init(nextimage_callback, settings);
  };
  AdGallerySlideshow.prototype = {
    start_link: false,
    stop_link: false,
    countdown: false,
    controls: false,

    settings: false,
    nextimage_callback: false,
    enabled: false,
    running: false,
    countdown_interval: false,
    init: function(nextimage_callback, settings) {
      var context = this;
      this.nextimage_callback = nextimage_callback;
      this.settings = settings;
    },
    create: function() {
      this.start_link = $('<span class="ad-slideshow-start">'+ this.settings.start_label +'</span>');
      this.stop_link = $('<span class="ad-slideshow-stop">'+ this.settings.stop_label +'</span>');
      this.countdown = $('<span class="ad-slideshow-countdown"></span>');
      this.controls = $('<div class="ad-slideshow-controls"></div>');
      this.controls.append(this.start_link).append(this.stop_link).append(this.countdown);
      this.countdown.hide();

      var context = this;
      this.start_link.click(
        function() {
          context.start();
        }
      );
      this.stop_link.click(
        function() {
          context.stop();
        }
      );
      $(document).keydown(
        function(e) {
          if(e.keyCode == 83) {
            // 's'
            if(context.running) {
              context.stop();
            } else {
              context.start();
            };
          };
        }
      );
      return this.controls;
    },
    disable: function() {
      this.enabled = false;
      this.stop();
      this.controls.hide();
    },
    enable: function() {
      this.enabled = true;
      this.controls.show();
    },
    toggle: function() {
      if(this.enabled) {
        this.disable();
      } else {
        this.enable();
      };
    },
    start: function() {
      if(this.running || !this.enabled) return false;
      var context = this;
      this.running = true;
      this.controls.addClass('ad-slideshow-running');
      this._next();
      this.fireCallback(this.settings.onStart);
      return true;
    },
    stop: function() {
      if(!this.running) return false;
      this.running = false;
      this.countdown.hide();
      this.controls.removeClass('ad-slideshow-running');
      clearInterval(this.countdown_interval);
      this.fireCallback(this.settings.onStop);
      return true;
    },
    _next: function() {
      var context = this;
      var pre = this.settings.countdown_prefix;
      var su = this.settings.countdown_sufix;
      clearInterval(context.countdown_interval);
      this.countdown.show().html(pre + (this.settings.speed / 1000) + su);
      var slide_timer = 0;
      this.countdown_interval = setInterval(
        function() {
          slide_timer += 1000;
          if(slide_timer >= context.settings.speed) {
            var whenNextIsShown = function() {
              // A check so the user hasn't stoped the slideshow during the
              // animation
              if(context.running) {
                context._next();
              };
              slide_timer = 0;
            };
            if(!context.nextimage_callback(whenNextIsShown)) {
              context.stop();
            };
            slide_timer = 0;
          };
          var sec = parseInt(context.countdown.text().replace(/[^0-9]/g, ''), 10);
          sec--;
          if(sec > 0) {
            context.countdown.html(pre + sec + su);
          };
        },
        1000
      );
    },
    fireCallback: function(fn) {
      if($.isFunction(fn)) {
        fn.call(this);
      };
    }
  };
})(jQuery);

