$(document).ready(function(){

  function animateForward(target){
    $(".next, .back").unbind("click");
    var width = target.children("li").outerWidth();
    var position = target.children("li:first").position();      
    var newspot = (position.left) - width;
    var total = target.outerWidth();
    if((total - width*5) < (position.left*-1)){
      current = position.left;
      target.stop().animate({"marginLeft" : "-"+(width*6)-20+"px"}, 300,function(){
        target.animate({"marginLeft" : "-"+(width*6)+"px"}, {queue:false, duration:300})
        bindIt();
      });
      return false;
    }
    target.animate({"marginLeft" : newspot+"px"}, 300, function(){
      bindIt();
    });
  }
 
  function animateBackward(target){
    $(".next, .back").unbind("click");
    
    var width = target.children("li").outerWidth();
    var position = target.children("li:first").position();
    var newspot = (position.left) + width;
    if((position.left)>=0){
      current = position.left;
      target.stop().animate({"marginLeft" : "20px"}, 300,function(){
        target.animate({"marginLeft" : "0px"}, {queue:false, duration:300});
        bindIt();
      });
      return false;
    }
    
    target.animate({"marginLeft" : (newspot)+"px"}, 300, function(){
      bindIt();
    });
  }
  
  function bindIt(){
    $(".next").bind("click", function(event){animateForward($(".tweet .tweet_list")); event.preventDefault();});
    $(".back").bind("click", function(event){animateBackward($(".tweet .tweet_list")); event.preventDefault();});
  }
  function scrollAnim(n, x){
    if(x == null) x =300;
  		$("html, body").animate({ scrollTop: (n) }, 800, 'swing', {queue: false});
  }

  var twitteroffset = $(".follow").offset();
  $(".twitarea .seeall").click(function(){
    var x = 200;
    if($(".tweet .tweet_holder").length > 0){
       x = 830; 
    }
    scrollAnim(twitteroffset.top - x);
  });
  
  $("#slideshow ul").cycle({
    pager:  '#pager',
    timeout: '8000',
    pagerAnchorBuilder: function(idx, slide) { 
        $title = slide.childNodes[1].alt;
        return '<li><a href="#">' + $title + '</a></li>'; 
    }
  });
  
  $("#pager li a").each(function(){
      $(this).hover(

        function(){
          var popup = $("#popup");
          var text = $(this).text();
          popup.text(text);
          var width = popup.outerWidth();
          
          offset = $(this).offset();
          popup.css({
            display : 'block',
            left: (offset.left - (width/2) +5)   +"px",
            top: (offset.top - 60)+"px"
          })
          popup.animate({opacity: 1, top: ((offset.top - 40)+"px")},{queue:false, duration:300});
        },
        function(){
          $("#popup").animate({opacity: 0},{queue:false, duration:300});
        }
      );
    });
  // end slide show
  
  
  $("body").append("<div id='popup'></div>");
  $("#popup").css({
    display:'none'
  });
  
  $("#mainevents>div:last").css({
    float : 'right',
    width : '274px'
  });
  $("#footer_nav li:not(:last)").append(" /");
  
  function contentSwitch(n, p, speed, target){
    if(speed == 0){
      target.html(n);
      return;
    }
    target.children().animate({ opacity:0 }, speed, function(){
    target.html(n).css({opacity:0}).animate({opacity:1}, speed);
          
    }); 
  }
    
  //store content.
  var events =  $("#newsevents div.events").html();
  var news =    $("#newsevents div.news").html();
    
  contentSwitch(events, news, 0, $("#newsevents"));
  
  $("#newsevents").before("<a class='eventsbutton' href='#'>Events</a><a class='newsbutton' href='#'>News</a>");
  $(".eventsbutton").addClass("active");
  $(".newsbutton").click(function(n){
    n.preventDefault();
    contentSwitch(news, events, 300, $("#newsevents"));
    $(this).addClass("active");
    $(".eventsbutton").removeClass("active");
  });
    
  $(".eventsbutton").click(function(n){
    n.preventDefault();
    contentSwitch(events, news, 300, $("#newsevents"));
    $(this).addClass("active");
    $(".newsbutton").removeClass("active");
  });
    
    
    // get 1st from dhbc without retweets and only 1 item
    $(".twitarea div").tweet({
        username: "downtownhalifax",
        join_text: "auto",
        count: 1,
        retweets: false,
        auto_join_text_default: null,
        auto_join_text_ed: null,
        auto_join_text_ing: null,
        auto_join_text_reply: null,
        auto_join_text_url: null,
        loading_text: "loading tweet...",
        template: "{text}"
    });
    
    // this is all for the twitter search at the bottom.
    $(".tweet .tweet_holder").tweet({
        username: "downtownhalifax",
        join_text: "auto",
        count: 10,
        auto_join_text_default: null,
        auto_join_text_ed: null,
        auto_join_text_ing: null,
        auto_join_text_reply: null,
        auto_join_text_url: null,
        loading_text: "loading tweets..."
    });
    
    // create scrolling aspect.
    
    // because there is no feedback via the tweet script we can not do a 
    // callback from it so we need to inject this data via a user action.
    var count = 1;
    
    $(".tweet_holder").after("<div class='buttons'><span class='back'>Back</span><span class='next'>Next</span></div>");

    bindIt();
    
    
    // search bar.
    
    var search = $("#searcharea").html();
    $("#searcharea").remove();
    $('body header').before("<div id='searcharea'>"+search+"</div>");
    
    $("li.search a").click(function(n){
      $("#searcharea").slideDown(100);
      $('body').css({borderTop: '0px'});
      $('header').css({position:'relative'});
      $('#slideshow, #headerimage').removeClass('toppadding');
      scrollAnim(0);
      n.preventDefault();
    });
    $("#searcharea .close").click(function(){
      $("#searcharea").slideUp(100);
      $('body').css({borderTop: '4px solid #00adda'});
      $('header').css({position:'fixed'});
      $('#slideshow, #headerimage').addClass('toppadding');
      
    })
    
    
    // 56487766@N04
    $("#photostream, #sidephoto").jflickrfeed({
      
      limit : 8,
      qstrings: {
        id : '56487766@N04'
      },
      itemTemplate: '<li>' + '<a href="{{image_b}}"><img src="{{image_s}}" alt="{{title}}" /></a>' + '</li>'
      
      
    });
    $(".twit a").click(function(n){
      var link = $(this).attr("href");
      //properties
      var width   = 574;
      var height  = 400;
      var left    = ($(window).width() - width) /2;
      var top     = ($(window).height() - height) /2;
      var params  = 'status=1' + ',width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',resizable=0';
      windowOpen(link, params);
      n.preventDefault();
      
    });
    //popup
    function windowOpen(n, x){  
      window.open(n, "newwindow", x);
    }

    //directory
    $("#directory tr:even td:even").addClass("both");
    $("#directory tr:even").addClass("shade");
    $("#busdir select").change(function(){
      var n = $(this).val();
      location.href = "/index.php/doingbusiness/business-directory/" + n;
    });
    $("#busdir select option")
    
    $("#archive").change(function(){
      var n = $(this).val();
      location.href = n;
    });
    
});








