$(document).ready(function(){
 
getInfo();

$('iframe#flickr').iframeAutoHeight();

//get latest tweet
$.getJSON("https://twitter.com/status/user_timeline/FTA_DOT.json?count=1 &callback=?",
        function(data, textStatus) {
        var public_tweets = data;
            var twt = public_tweets[0];
            var text = twt.text;
            var text = linkify(text);
            var id = twt.id_str;
            var twtDate = twt.created_at;
            var twtDate = $.format.date(twtDate, "MMMM dd");
             
            var elm = '<p>' + text + '</p><p><a href="http://twitter.com/#!/FTA_DOT/status/' + id + '">' + twtDate + '</a> | <a href="http://www.twitter.com/FTA_DOT">@FTA_DOT</a></p>';
            $('#tweet').html(elm);
        });


//new icon
$('[class^="new_"]').each(function() {
    var className = $(this).attr("class"); 
    var partsArray = className.split('_');
    var d = new Date(partsArray[1]);
    var now = new Date();
    if(d > now){
        $(this).prepend('<img src="/images/content_images/new.png" hspace="2" align="left">');
    }
});

//Exit Page

$('a:[href*=".com"]').each(function(){
    var pageURL = window.location.href;
    if (pageURL.indexOf( "exit.php" ) == -1){
        var href=(this);
        $(this).attr("href","/exit.php?url="+href);
    }
});



// Reset Font Size
 var originalFontSize = $('#txtContent').css('font-size');
 $(".resetFont").click(function(){
 $('#txtContent').css('font-size', originalFontSize);
 });

 // Increase Font Size
 $(".increaseFont").click(function(){
  var currentFontSize = $('#txtContent').css('font-size');
 var currentFontSizeNum = parseFloat(currentFontSize, 10);
   var newFontSize = currentFontSizeNum*1.2;
 $('#txtContent').css('font-size', newFontSize);
 return false;
 });
 
// Decrease Font Size
 $(".decreaseFont").click(function(){
  var currentFontSize = $('#txtContent').css('font-size');
 var currentFontSizeNum = parseFloat(currentFontSize, 10);
   var newFontSize = currentFontSizeNum*0.8;
 $('#txtContent').css('font-size', newFontSize);
 return false;
 });

 $("#contact_region").click(function(){
var innerPosition =  $('.contact_inner').css('left');
if(innerPosition =='35px')   $('.contact_inner').css('left', '-9999px');
else $('.contact_inner').css('left', '35px');
 return false;
 });

 $('body').click(function(){
var innerPosition =  $('.contact_inner').css('left');
if(innerPosition !='-9999px')   $('.contact_inner').css('left', '-9999px');
 });


 $("#contact_region_content").click(function(){
var innerPosition =  $('.contact_inner').css('left');
if(innerPosition =='10px')   $('.contact_inner').css('left', '-9999px');
else $('.contact_inner').css('left', '10px');
 return false;
 });

 $("#contact_region_page").click(function(){
var innerPosition =  $('.contact_inner').css('left');
if(innerPosition =='10px')   $('.contact_inner').css('left', '-9999px');
else $('.contact_inner').css('left', '10px');
 return false;
 });



});
 
//Expand-Collapse
function expandCollapse() {
for (var i=0; i<expandCollapse.arguments.length; i++) {
var element = document.getElementById(expandCollapse.arguments[i]);
element.style.display = (element.style.display == "none") ? "block" : "none";
    }
}

//Display for 'Were You Looking For?'
function were_display() {
    var category = document.getElementById('were_you_looking_content');
    var other_category = document.getElementById('more_info_content');
    var container = document.getElementById('were_you_looking');
    var other_container = document.getElementById('more_info');
    if (category.className=="show") {
        category.className="hide";
        container.style.backgroundPosition = "right 4px" }
         else {
        category.className="show";
        container.style.backgroundPosition = "right -15px";
        if(other_container){
            other_category.className="hide";
            other_container.style.backgroundPosition = "right 4px";
        }
    }
}

//Display for "For More Information"
function info_display() {
    var category = document.getElementById('more_info_content');
    var other_category = document.getElementById('were_you_looking_content');
    var container = document.getElementById('more_info');
    var other_container = document.getElementById('were_you_looking');
    if (category.className=="show") {
        category.className="hide";
        container.style.backgroundPosition = "right 4px" } else {
        category.className="show";
        container.style.backgroundPosition = "right -15px";
        if(other_container){
            other_category.className="hide";
            other_container.style.backgroundPosition = "right 4px";
        }
    }
}


function flickr(url){
var part=url.split("/")
var photoID = (part[5]);
    $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=17b5ccc1f31e131a7451782615bdc01e&photo_id=" + photoID + "&format=json&nojsoncallback=1", function (data) {
             $("#flickr").html("<img src='http://farm" + data.photo.farm + ".static.flickr.com/" + data.photo.server +  "/" + data.photo.id + "_" + data.photo.secret + "_m.jpg' width='230px' /><p class='caption'>" + data.photo.description._content) + "</p>";
});
};

function flickr_rand(){
$.getJSON("http://api.flickr.com/services/feeds/groups_pool.gne?id=1612348@N20&lang=en-us&display=random&format=json&nojsoncallback=1", function (data){                                                                                                                                   
    var i = -1;
    $.each(data.items, function() { 
          i++; 
        });
    var rndNum = Math.floor(Math.random() * i);
    var link = data.items[rndNum].link;
    var part=link.split("/");
    var photoID = (part[5]);
    $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=17b5ccc1f31e131a7451782615bdc01e&photo_id=" + photoID + "&format=json&nojsoncallback=1", function (data) {
             $("#flickr").html("<img src='http://farm" + data.photo.farm + ".static.flickr.com/" + data.photo.server +  "/" + data.photo.id + "_" + data.photo.secret + "_m.jpg'  width='230px' /><br />" + data.photo.description._content);
});
});     
};

function linkifyx(text) {
    text = text.replace(/(https?:\/\/\S+)/gi, function (s) {
        return '<a href="' + s + '">' + s + '</a>';
    });
    text = text.replace(/(^|)@(\w+)/gi, function (s) {
        return '<a href="http://twitter.com/' + s + '">' + s + '</a>';
    });
    text = text.replace(/(^|)#(\w+)/gi, function (s) {
        return '<a href="http://search.twitter.com/search?q=' + s.replace(/#/,'%23') + '">' + s + '</a>';
     });
    return text;
}

function linkify(text){
    if (text) {
        text = text.replace(
            /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
            function(url){
                var full_url = url;
                if (!full_url.match('^https?:\/\/')) {
                    full_url = 'http://' + full_url;
                }
                return '<a href="' + full_url + '">' + url + '</a>';
            }
        );
    }
    return text;
}


function desaturate(img) {
        var img2 = Pixastic.process(img, "desaturate");
        img2.onmouseout = function() {
            Pixastic.revert(this);
    }
}
 

//find in page
function findInPage(str) {
var NS4 = (document.layers);    // Which browser?
var IE4 = (document.all);
var win = window;    // window to search.
var n   = 0;
  
var txt, i, found;
  if (str == "")
    return false;
  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.
  if (NS4) {
    // Look for match starting at the current point. If not found, rewind
    // back to the first match.
    if (!win.find(str))
      while(win.find(str, false, true))
        n++;
    else
      n++;
    // If not found in either direction, give message.
    if (n == 0)
      alert("Not found.");
  }
  if (IE4) {
    txt = win.document.body.createTextRange();
    // Find the nth match from the top of the page.
    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }
    // If found, mark it and scroll it into view.
    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }
    // Otherwise, start over at the top of the page and find first match.
    else {
      if (n > 0) {
        n = 0;
        findInPage(str);
      }
      // Not found anywhere, give message.
      else
        alert("Not found.");
    }
  }
  return false;
}

function getInfo()
{
    //If this is the homepage, then do nothing
    if(window.opener != null && document.getElementById("error")) {
        title = window.opener.document.title;
        url = window.opener.location.href;   
        document.error.txtTitle.value = title;
        document.error.txtURL.value = url;
    }
}
//Open error submission form in new window
function openPage()
{
    window.open("/about_FTA_errorForm.html");
}
