// fix clear type bug in IE

(function($) 
{
	$.fn.ClearTypeFadeTo = function(options) {
		if (options)
			$(this)
				.show()
				.each(function() {
					if (jQuery.browser.msie) {
						// Save the original background color
						$(this).attr('oBgColor', $(this).css('background-color'));
						// Set the bgColor so that bold text renders correctly (bug with IE/ClearType/bold text)
						$(this).css({ 'background-color': (options.bgColor ? options.bgColor : '#ffffff') })
					}
				})
				.fadeTo(options.speed, options.opacity, function() {
					if (jQuery.browser.msie) {
						// ClearType can only be turned back on if this is a full fade in or
						// fade out. Partial opacity will still have the problem because the
						// filter style must remain. So, in the latter case, we will leave the
						// background color and 'filter' style in place.
						if (options.opacity == 0 || options.opacity == 1) {
							// Reset the background color if we saved it previously
							$(this).css({ 'background-color': $(this).attr('oBgColor') }).removeAttr('oBgColor');
							// Remove the 'filter' style to restore ClearType functionality.
							$(this).get(0).style.removeAttribute('filter');
						}
					}
					if (options.callback != undefined) options.callback();
				});
	};

	$.fn.ClearTypeFadeIn = function(options) {
		if (options)
			$(this)
				.css({ opacity: 0 })
				.ClearTypeFadeTo({ speed: options.speed, opacity: 1, callback: options.callback });
	};

	$.fn.ClearTypeFadeOut = function(options) {
		if (options)
			$(this)
				.css({ opacity: 1 })
				.ClearTypeFadeTo({ speed: options.speed, opacity: 0, callback: options.callback });
	};
})(jQuery);

/**
 *  VIDEO PLAYER
 */ 
function embedHulu(render){
  return '\
    <div id="player_'+dID+'" class="write_video_append">\
      <div class="write_video_player">\
        '+render+'\
      </div>\
      <div class="write_video_option">\
        <ul>\
          <li onclick="removeVideo(\''+dID+'\', \''+vID+'\', \''+sID+'\');">remove video</li>\
        </ul>\
      </div>\
    </div>';
}

function embedYouTube(dID, video_id, sID){
  var vWidth = 641;
  var vHeight = 362; 
  var vID = video_id;
  
  return '\
    <div id="player_'+dID+'" class="write_video_append">\
      <div class="write_video_player">\
        <object width="'+vWidth+'" height="'+vHeight+'"><param name="movie" value="http://www.youtube.com/v/'+video_id+'&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+video_id+'&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+vWidth+'" height="'+vHeight+'"></embed></object>\
      </div>\
      <div class="write_video_option">\
        <ul>\
          <li onclick="removeVideo(\''+dID+'\', \''+vID+'\', \''+sID+'\');">remove video</li>\
        </ul>\
      </div>\
    </div>';
}

function embedVimeo(dID, video_id, sID){
  var vWidth = 641;
  var vHeight = 362; 
  var vID = video_id;
  return '\
    <div id="player_'+dID+'" class="write_video_append">\
      <div class="write_video_player">\
        <object width="'+vWidth+'" height="'+vHeight+'"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+video_id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+video_id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+vWidth+'" height="'+vHeight+'"></embed></object>\
      </div>\
    </div>';
}

function embedVideoPlayer(video_type, video_id, obj){
  $player = $("#video_player_" + obj);
  $gallery = $("#video_player_gallery_" + obj);
  
  openPlayer($player, $gallery, obj);
  
  switch(video_type)
  {
    case "0":
      $player.oembed("http://www.youtube.com/watch?v=" + video_id);
      break;
      
    case "1":
      $player.oembed("http://www.vimeo.com/" + video_id);
      break;
      
    case "3":
      $player.oembed("http://www.hulu.com/watch/" + video_id);
      break;
  }
}

function getVimeoJson(vID, obj){
  $.getJSON("http://oohembed.com/oohembed/?width=700&maxHeight=400&format=json&url=?url=http%3A//vimeo.com/"+vID+"&callback=?",
    function(data){
      $("#video_player_gallery_" + obj).append("<img style='border: 3px solid #000; padding: 0px;' src='"+data.thumbnail_url+"' />");
    }
  );
}

function openPlayer($player, $gallery, obj)
{
  $openButton = $("#video_player_open_" + obj);
  $closeButton = $("#video_player_close_" + obj);
  
  $closeButton.show ();
  $openButton.hide();
  
  $gallery.hide();
  $player.show();
}
function closeVideoPlayer(obj)
{
  $player = $("#video_player_" + obj);
  $gallery = $("#video_player_gallery_" + obj);
  $openButton = $("#video_player_open_" + obj);
  $closeButton = $("#video_player_close_" + obj);
  
  $player.empty();
  $player.append("<img src='resources/template/css/images/loading.gif'>");
  $player.hide();
  $gallery.show();
  $closeButton.hide();
  $openButton.show();
  
  $("#sub_v3_video_player_"+obj).removeClass("highlight");
}

/*
$(document).ready(function()
{
 
});
*/


function bump(value, where, mode){
  //alert(value + " " + where + " " + mode);
  $.post("lib/Bumpley/BumpleyBump_js.php", {
      //in_bumley:"",
      value:value,
      where:where,
      mode:mode
    },

    function(data){
      //alert(data);
      if( data=='no'){
        //error 		
      } else {
        if (where == "submission_bump"){
          //$('#bump_holder_' + 
          $('#submission_bump_holder_' + value).ClearTypeFadeOut({ 
              speed: 400, callback: function() { 
                document.getElementById('submission_bump_holder_' + value).innerHTML = data + " bumps";
                $('#submission_bump_holder_' + value).ClearTypeFadeIn({ speed: 800, callback: function() 
                  {
                  } 
                });
              } 
          }); 
        } else if (where == "submission_review_bump"){
          $("#review_id_" + value).fadeTo(250, 0.0, function () {
            document.getElementById('review_bump_holder_' + value).innerHTML = data + "<br>bumps";
            $("#review_id_" + value).fadeTo(250, 1, function () {
            
            });  
          });          
        } else if (where == "submission_lead_bump") {
          $("#submission_lead_bump_holder_" + value).fadeTo(250, 0.0, function () {
            document.getElementById('submission_lead_bump_holder_' + value).innerHTML = data + "<br>Bumps";
            $("#submission_lead_bump_holder_" + value).fadeTo(250, 1, function () {
            
            });                  
          }); 
        } else if (where == "article_gallery_bump") {
          /*
          $("#submission_lead_bump_holder_" + value).fadeTo(250, 0.0, function () {
            document.getElementById('submission_lead_bump_holder_' + value).innerHTML = data + "<br>Bumps";
            $("#submission_lead_bump_holder_" + value).fadeTo(250, 1, function () {
            
            });                  
          });
           */
           alert('image bumped');
        }
      }
    }
  );
}

//alert('asd');
function flag(value){
  $.post("lib/Bumpley/BumpleyBump_js.php", {
      value:value,
      where:"f_s"
    },

    function(data){
      if (data == "track"){
        doFaceBox("You've already flagged this article.");
      } else if (data == "1"){
        doFaceBox("Thank you for sharing your concerns.");
      } else if (data == "logg"){
        doFaceBox("You must be signed in to flag this article.");
      }
      
      alert(data);
    }
  );
}



function sf(value, ac){
  $.post("lib/Bumpley/BumpleyBump_js.php", {
      value:value,
      action:ac,
      where:"st_f"
    },

    function(data){
      
      if (data == "true"){
        // done
        doFaceBox("sf 1");
      } else if (data == "logg"){
        doFaceBox("Log in");
      }
    }
  );
}

function bumpIndex(value, where, mode, id_holder){

            

  /**
   * if server is taking more than 1.5 seconds to respond, show loading...
   */     
  var inputDelayer=delayTimer(1500);
  inputDelayer(function(){
    jQuery.facebox("<div style='padding: px; border: 0px solid #ddd; border-bottom: 0px;'><img src='resources/template/css/images/loading.gif' /></div>");
  });

            

  $.post("lib/Bumpley/BumpleyBump_js.php", {
      value:value,
      where:where,
      mode:mode
    },

    function(data){
      // close the loading box if opened and clear the timer
      inputDelayer();
      jQuery.facebox.close();
      
      if( data=='no'){
        //error 		
      } else if (data == 'track') {
        switch(where)
        {
          case "submission_bump":
            doFaceBox(b_bumpArticle);
            break;
            
          case "submission_review_bump":
            doFaceBox(b_bumpArticleReview);
            break;        
            
          default:    
            doFaceBox(b_bumpGeneric);
            break;
        }
        
      } else {
      
        if (where == "submission_bump"){
          $obj = $('#'+id_holder);
          $obj.removeClass("new_bump");
          $obj.addClass("new_bump_disabled");
          //$obj.effect("pulsate", { times:1 }, 300);
            
            if (data > 1){
              bTitle = " Bumps";
            } else {
              bTitle = " Bump";
            }
          $('#bumpStatHolder_' + value).empty()
            .append("+" + data + bTitle).effect("highlight", { color:"#8fdaf4"}, "slow");  
        }
      } 
      //end
      
    }
  );
}

function bumpInside(value, where, mode, id_holder){
  // handler
  
  if (!$.browser.msie){
    $('#' + id_holder).fadeTo("normal", 0.33);
  } else {
    $('#' + id_holder).fadeTo("normal", 0.33);
  }
  
  $.post("lib/Bumpley/BumpleyBump_js.php", {
      value:value,
      where:where,
      mode:mode
    },

    function(data){
      
      if( data=='no'){
        //error 		
      } else if (data == 'track') {
        switch(where)
        {
          case "submission_bump":
            doFaceBox(b_bumpArticle);
            break;
            
          case "submission_review_bump":
            doFaceBox(b_bumpArticleReview);
            break;
            
          case "profile_bump":
            doFaceBox("You can only Bump a profile once.");
            break;                           
            
          default:    
            doFaceBox(b_bumpGeneric);
            break;
        }
        
      } else {
        //alert(data);
        if (where == "submission_bump"){

          if (data > 0)
          {
            if (data == 1)
            {
              message = "<font style='color: #fff;'>1 Bump</font>";
            } else {
              message = "<font style='color: #fff;'>"+data+" Bumps</font>";
            }           
          } else {
            
          }
        
          if ($.browser.msie){
            // middle finger internet explorer hack.

            document.getElementById('submission_bump_holder_' + value).innerHTML = "<center>"+message+"</center>";
          } else {
            $('#submission_bump_holder_' + value).ClearTypeFadeOut({ 
                speed: 300, callback: function() { 
                  document.getElementById('submission_bump_holder_' + value).innerHTML = "<center>"+message+"<center>";
                  $('#submission_bump_holder_' + value).ClearTypeFadeIn({ speed: 300, callback: function() {
                    } 
                  });
              } 
            });
          }
        } else if (where == "profile_bump"){

          if (data > 0)
          {
            if (data == 1)
            {
              message = "<font style='color: #fff;'>1 Bump</font>";
            } else {
              message = "<font style='color: #fff;'>"+data+" Bumps</font>";
            }           
          } else {
            
          }
        
          if ($.browser.msie){
            // middle finger internet explorer hack.

            document.getElementById('submission_bump_holder_').innerHTML = "<center>"+message+"</center>";
          } else {
            $('#submission_bump_holder_').ClearTypeFadeOut({ 
                speed: 300, callback: function() { 
                  document.getElementById('submission_bump_holder_').innerHTML = "<center>"+message+"<center>";
                  $('#submission_bump_holder_').ClearTypeFadeIn({ speed: 300, callback: function() {
                    } 
                  });
              } 
            });
          }          
        }
      }
    }
  );
}

function bumpSubmissionGallery(value, image_id, mode, id_holder){
  //alert(value + " " + where + " " + mode);
  //alert(id_holder);
  
  // handler
  if (!$.browser.msie){
    $('#bump_action_holder').fadeTo("normal", 0.33);
  } else {
    $('#bump_action_holder').fadeTo("normal", 0.33);
  }
  
  $.post("lib/Bumpley/BumpleyBump_js.php", {
      value:value,
      image_id:image_id,
      where:"article_gallery_bump",
      mode:mode
    },

    function(data){
      
      if( data=='no'){
        //error 		
      } else if (data == 'track') {
        doFaceBox(b_bumpTrackGeneric);
      } else {
        //alert(data);
     
        
          if ($.browser.msie){
            // middle finger internet explorer hack.
            document.getElementById('submission_bump_holder_' + value).innerHTML = "<center><font style=\'color: #ff00ac\'>" + data + " BUMPS</font></center>";
          } else {
            $('#submission_bump_holder_' + value).ClearTypeFadeOut({ 
                speed: 300, callback: function() { 
                  document.getElementById('submission_bump_holder_' + value).innerHTML = "<center><font style=\'color: #ff00ac\'>" + data + " BUMPS</font><center>";
                  $('#submission_bump_holder_' + value).ClearTypeFadeIn({ speed: 300, callback: function() {
                    } 
                  });
              } 
            });
          }

      }
      
    }
  );
}



function addToTrack(key, value, id_holder){
  
  $.post("lib/Bumpley/BumpleyFavorites_js.php", {
      meta_key:key,
      meta_value:value
    },

    function(data){
      switch(data)
      {
        case "ok":
          
          if (key == 14)
          {
            iconSwitch = "heart_add.png";
          } else if (key == 15) {
            iconSwitch = "door_in.png";
          }
          
          if (!$.browser.msie)
          {
            $('#' + id_holder).fadeTo("fast", 0, function(){
              document.getElementById(id_holder).innerHTML = "<img src='resources/template/css/images/icons/silk/"+iconSwitch+"'>";
              $('#' + id_holder).fadeTo("fast", 1);
            });
          } else {
            document.getElementById(id_holder).innerHTML = "<img src='resources/template/css/images/icons/silk/"+iconSwitch+"'>";
          }
          break;
          
        case "track_closet":
          //alert('You\'ve already added this into your closet.');
          doFaceBox(b_closetTrack)
          break;
          
        case "track_favorite":
          //alert('You\'ve already added this into your favorites.');
          break;
          
        case "no":
          //alert('You must be logged in to be able to do that.');
          doFaceBox(b_closetLoggedIn)
          break;          
      }
    }
  );
}
