// JavaScript Document
// Global Variables
var i = 0;
var total;
/*
$(document).ready(function() {
       //total = $("img:hidden").length;
       total = imgs.length;
       showImage();
});
*/
// eventHandler Function for onLoad
function load() {
  total = $("img:hidden").length;
  showImage();
}

function test() {
  total = $("img:hidden").length;
  showImage();
}

// callBackHandler Function for JQuery fadeIn
function showImage() {
  if (i < total) {
    $("img:hidden:first").fadeIn(700, showImage);
    i++;
  }
}

// Function to write SWFObject
function writeSWFObject(id, swf, width, height, version, flashvars, params, attributes) {
  if (flashvars == null) flashvars = {};
  if (params == null) params = {};
  if (attributes == null) attributes = {};
  swfobject.embedSWF(swf, id, width, height, version, "swf/expressInstall.swf", flashvars, params, attributes);
}

$(function() {
  $('a[href*=#]').click(function() {
    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length && target;
      if (target.length) {
        var sclpos = 100;
        var scldurat = 1300;
        var targetOffset = target.offset().top - sclpos;
        $('html,body').animate({ scrollTop: targetOffset }, { duration: scldurat, easing: "easeOutQuart" });
        return false;
      }
    }
  });
});

function showHide(showKey, hideKey) {
  $(hideKey).hide();
  $(showKey).toggle(1000);
}

function cleanHTML(key) {
  $(key).html("");
}

function tweets_url(page) {
  page = page || 1
  return "twitter.php?page=" + page + "&callback=?";
  //return "http://twitter.com/statuses/user_timeline/designroyale.json?page=" + page + "&callback=?";
}

tweets_per_page = 10
function construct_tweets(data, container, current_page) {
  current_page = current_page || 1
  tweet_data = ''
  tweets = data.slice((current_page - 1) * tweets_per_page, tweets_per_page * current_page)

  for (iloop = 0; iloop < tweets.length; iloop++) {
    tweet = tweets[iloop]
    tweet_time = parse_twitter_date(tweet.created_at)
    tweet_data += '<div class="tweet-row">'
    tweet_data += '  <div class="tweet-time">' + $.PHPDate("G:i A M jS", tweet_time) + ' from ' + tweet.source + '</div>'
    tweet_data += '  <div class="tweet-content">' + tweet.text + '</div>'
    tweet_data += '  <div class="separator"></div>'
    tweet_data += '</div> <!-- tweet-row -->'
  }
  
  $('#' + container).html(tweet_data)
}

function parse_twitter_date(created_at) {
  if (navigator.userAgent.indexOf('MSIE') == -1)
    return new Date(created_at)
  else {
    tweet_time = new Date(created_at.replace('+0000 ', ''))
    tweet_time.setHours(tweet_time.getHours() - (tweet_time.getTimezoneOffset() / 60))
    return tweet_time
  }
}

function parse_news_date(date) {
  date_parts = date.split('/')
  return new Date(new Number(date_parts[2]), new Number(date_parts[1]) - 1, new Number(date_parts[0]))
}

function construct_pagination(total, items_per_page, current_page, page_url) {
  no_of_pages = Math.ceil(total / items_per_page)
  //  pagination = '<div class="pagination">'
  pagination = ''
  for (iloop = 1; iloop <= no_of_pages; iloop++) {
    current = ''
    url = page_url + '(' + iloop + ')'
    if (current_page == iloop) {
      current = 'current'
      url = 'void(0)'
    }
    pagination += '<a href="javascript:' + url + '" class=' + current + '>' + iloop + '</a>'
  }
//  pagination += '</div>'

  return pagination
}

function get_footer_tweets() {
    $.getJSON(tweets_url(), function(data) {
      twitter_container = 'twitter-container'
      construct_tweets(data, twitter_container)
      $('#' + twitter_container).jScrollPane({ scrollbarWidth: 7 });

      $('#footer .jScrollPaneTrack').css('top', '5px')
    })
}

function initialize_header() {
  url_parts = location.href.split('/')
  current_page = url_parts[url_parts.length - 1]
  

  var selected = false;
  $('#header .right-block div a').each(function(iloop, link) {
  if (current_page && $(link).attr('href').indexOf(current_page) >= 0) {
      $(link).find('span').addClass('highlighted');
      selected = true;
  }
  })
  if (selected == false) {
    $('#header .right-block div a:first').find('span').addClass('highlighted');
  }
}


function scrollToTop() {
	$.scrollTo('#main-container', 500);
}