//http://www.developersnippets.com/2010/12/04/how-to-add-wmodetransparent-for-flash-object-using-jquery-and-native-javascript/
function fix_flash() {
//    var embeds = document.getElementsByTagName('embed');
//    for(i=0; i<embeds.length; i++)  {
//        embed = embeds[i];
//        var new_embed;
//        if(embed.outerHTML) {
//            var html = embed.outerHTML;
//            if(html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
//                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i,"wmode='transparent'");
//            else 
//                new_embed = html.replace(/<embed\s/i,"<embed wmode='transparent' ");
//            embed.insertAdjacentHTML('beforeBegin',new_embed);
//            embed.parentNode.removeChild(embed);
//        } else {
//            new_embed = embed.cloneNode(true);
//            if(!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase()=='window')
//                new_embed.setAttribute('wmode','transparent');
//            embed.parentNode.replaceChild(new_embed,embed);
//        }
//    }
//    var objects = document.getElementsByTagName('object');
//    for(i=0; i<objects.length; i++) {
//        object = objects[i];
//        var new_object;
//        if(object.outerHTML) {
//            var html = object.outerHTML;
//            if(html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
//                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i,"<param name='wmode' value='transparent' />");
//            else 
//                new_object = html.replace(/<\/object\>/i,"<param name='wmode' value='transparent' />\n</object>");
//            var children = object.childNodes;
//            for(j=0; j<children.length; j++) {
//                if(children[j].getAttribute('name').match(/flashvars/i)) {
//                    new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i,"<param name='flashvars' value='"+children[j].getAttribute('value')+"' />");
//                }
//            }
//            object.insertAdjacentHTML('beforeBegin',new_object);
//            object.parentNode.removeChild(object);
//        }
//    }
}


$(document).ready(function() {

 $("#info-request").colorbox({'width': 470, 'height': 440, title:'Request more information', iframe:true, transition:'none', opacity:'0.2', overlayClose:false, rel:'nofollow'})
 $("#flickr a").colorbox({ rel: 'flickr', scalePhotos: true, maxWidth: '90%', maxHeight: '90%', transition:'none', opacity:'0.6' });
 $("a[rel=single]").colorbox({'width': 470, 'height': 520, iframe:true, transition:'none', opacity:'0.2', overlayClose:false, rel:'nofollow'});
 $("a[rel=preview]").colorbox({'title': 'Preview', 'width': '80%', 'height': '80%', iframe:true, transition:'none', opacity:'0.2', overlayClose:false, rel:'nofollow'});

 fix_flash();

 $("#visit-website").click( function () {
  url = $(this).attr("href");
  window.open(url, 'External'); return false
 });

 $("#today").html(today());

 $(".item div span:last-child").css("border-right","0px") 

 $('.newsonline').ThreeDots({ max_rows: 2, alt_text_e: true, alt_text_t: true });

 $('.photo').bind('mouseenter mouseleave', function(e){
  var fade_type = e.type == 'mouseenter' ? 'show' : 'hide';
  $(this).find('div').eq(1)[fade_type]();
 })

 $('input,select,textarea').not("#date").focus(function() {   
  $(this).addClass("focus");
 });   
 $('input,select,textarea').not("#date").blur(function() {   
  $(this).removeClass("focus");
 });

 $(".link-block").hover(
  function () {
   $(this).addClass("hi");
  }, 
  function () {
   $(this).removeClass("hi");
  }
 );

 if ( $.cookie('username') == '' || $.cookie('member') == '' ){
  $("#logout").hide();
  $("#userlogin").show();
 } else if ( $.cookie('username') == null || $.cookie('member') == null ){
  $("#logout").hide();
  $("#userlogin").show();
 } else {
  $("#username").html($.cookie('username'));
  $("#userlogin").hide();
  $("#logout").show();
 }

 $("a[rel='lightbox']").colorbox({ scalePhotos: true, maxWidth: '90%', maxHeight: '90%', transition:'none', opacity:'0.6' });

 $("#twitter").click( function () {
  url = "http://twitter.com/share?url=" + href + "&amp;text=" + name + "&amp;via=hospitalitynet";
  window.open(url, 'Facebook','toolbar=no,width=550,height=300'); return false;
 });

 $("#facebook").click( function () {
  url = "http://www.facebook.com/sharer.php?u=" + href + "&amp;t=" + name + "&amp;src=hospitalitynet";
  window.open(url, 'Facebook','toolbar=no,width=550,height=350'); return false;
 });

 $("#linkedin").click( function () {
  url = "http://www.linkedin.com/shareArticle?mini=true&amp;url=" + href + "&amp;title=" + name + "&amp;summary=&amp;source=Hospitality Net";
  window.open(url, 'LinkedIn','toolbar=no,width=550,height=400'); return false;
 });

 $("#delicious").click( function () {
  url = "http://www.delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=" + href + "&amp;title=" + name;
  window.open(url, 'Delicious','toolbar=no,width=650,height=400'); return false;
 });

 $("#digg").click( function () {
  url = "http://digg.com/submit?url=" + href + "&amp;title=" + name;
  window.open(url, 'Digg'); return false
 });

 $("#stumbleupon").click( function () {
  url =  "http://www.stumbleupon.com/submit?url=" + href + "&amp;title=" + name;
  window.open(url, 'StumbleUpon'); return false;
 });

 $("#technorati").click( function () {
  url =  "http://technorati.com/faves?add=" + url;
  window.open(url, 'Technorati'); return false;
 });

 $(".stock").each(function(index) {
  var $this = $(this); 
  symbol = $this.attr("id");
  $.yql( "select PreviousClose, Change, ChangeinPercent from yahoo.finance.quotes where symbol = '" + symbol + "'",
   function (data){
    var PreviousClose = data.query.results.quote.PreviousClose;
    var Change        = data.query.results.quote.Change;
    var PercentChange = data.query.results.quote.ChangeinPercent;
    var Trade         = parseFloat(PreviousClose) + parseFloat(Change);
    var Class         = "stock-up";
    if (Change.substring(0, 1) == "-") {
     var Class        = "stock-down";
    }
    PercentChange     = PercentChange.replace(/\+/, "");
    var StockInfo     = ' <span class="' + Class + '">' + addCommas(Trade.toFixed(2)) + ' (' + PercentChange + ')</span>'; 
    $($this).append( StockInfo );
   }
  );
 });

});

function hide(div) { $(div).hide(); }
function show(div) { $(div).show(); }

function addCommas(nStr)  {  
 nStr   += '';  
 x       = nStr.split('.');  
 x1      = x[0];  
 x2      = x.length > 1 ? '.' + x[1] : '';  
 var rgx = /(\d+)(\d{3})/;  
 while (rgx.test(x1)) {  
  x1     = x1.replace(rgx, '$1' + ',' + '$2');  
 }  
 return x1 + x2;  
}

function today() {
 var this_weekday_name_array = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
 var this_month_name_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
 var this_date_timestamp=new Date();
 var this_weekday = this_date_timestamp.getDay();
 var this_date = this_date_timestamp.getDate();
 var this_month = this_date_timestamp.getMonth();
 var this_date_string = this_weekday_name_array[this_weekday] + "<br/>" + this_month_name_array[this_month] + " " + this_date;
 return this_date_string;
}

function getUrlVars() {
 var vars = [], hash;
 var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&amp;');
 for(var i = 0; i < hashes.length; i++){
  hash = hashes[i].split('=');
  vars.push(hash[0]);
  vars[hash[0]] = hash[1];
 }
 return vars;
}

$(function(){
 var offsetX = 20;
 var offsetY = 10;
 $('a.preview').hover(function(e){ 
  var src = $(this).find('img').attr('src');
  $('<img id="preview" src="' + src + '" alt="image" />').css({'top':e.pageY + offsetY,'left':e.pageX + offsetX}).appendTo('body');
  }, function(){
  $('#preview').remove();
 });
 $('a.preview').mousemove(function(e){
  $('#preview').css({'top':e.pageY + offsetY,'left':e.pageX + offsetX});
 });
});

var paircount = 0;
function initImage(obj) {
 obj.onload = null;
 var $newthis = $(obj);
 if ($.browser.msie) {
  $newthis = $newthis.desaturateImgFix();
 }
 $newthis.addClass("pair_" + ++paircount);
 var $cloned = $newthis.clone().attr('id', '');
 $cloned.get(0).onload = null;
 $cloned.insertAfter($newthis).addClass("color").hide();
 $newthis = $newthis.desaturate();
 $newthis.bind("mouseenter mouseleave", desevent);
 $cloned.bind("mouseenter mouseleave", desevent);
};
function desevent(event) {}

function twitter(){
 url = "http://twitter.com/share?url=" + href + "&amp;text=" + name + "&amp;via=hospitalitynet";
 window.open(url, 'Facebook','toolbar=no,width=550,height=300'); return false
}

function facebook(){
 url = "http://www.facebook.com/sharer.php?u=" + href + "&amp;t=" + name + "&amp;src=hospitalitynet";
 window.open(url, 'Facebook','toolbar=no,width=550,height=350'); return false
}

function linkedin(){
 url = "http://www.linkedin.com/shareArticle?mini=true&amp;url=" + href + "&amp;title=" + name + "&amp;summary=&amp;source=Hospitality Net";
 window.open(url, 'LinkedIn','toolbar=no,width=550,height=400'); return false
}

function google(){
 url = "https://www.google.com/bookmarks/mark?op=add&amp;bkmk=" + href + "&amp;title=" + name + "&amp;annotation=";
 window.open(url, 'Google'); return false
}

function yahoo(){
 url = "http://myweb2.search.yahoo.com/myresults/bookmark?t=" + name + "&amp;u=" + href;
 window.open(url, 'Yahoo'); return false
}

function delicious(){
 url = "http://www.delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url=" + href + "&amp;title=" + name;
 window.open(url, 'Delicious','toolbar=no,width=650,height=400'); return false
}

function digg(){
 url = "http://digg.com/submit?url=" + href + "&amp;title=" + name;
 window.open(url, 'Digg'); return false
}

function stumbleupon(){
 url =  "http://www.stumbleupon.com/submit?url=" + href + "&amp;title=" + name;
 window.open(url, 'StumbleUpon'); return false
}

function technorati(){
 url =  "http://technorati.com/faves?add=" + url;
 window.open(url, 'Technorati'); return false
}

function setCookie(name,value){
 $.cookie(name, value, { path: '/' });
}

function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

