// starting the script on page load
$(document).ready(function(){
   $.each($("a[href*='greenvillehd.com']"),function(i,el){
      $(this).siblings("img[src*='video_icon_small']").wrap("<a href='"+this.href+"' target='"+this.target+"'></a>");
   });
   
	screenshotPreview();
	
   // toggles the more_info box on clicking the More Info &darr; link 
   $('.toggle').click(function(){
     if ($(this).next('blockquote').length > 0) {
        $(this).next('blockquote').toggle(400);
        $(this).toggleClass("up").toggleClass("down");
     } else if ($(this).next('.toggle_content').length > 0) {
        $(this).next('.toggle_content').toggle(400);
        $(this).toggleClass("up").toggleClass("down");
     }
     return false;
   }).toggleClass("down");

   //hide the div's to begin with
   $('.toggle').next().hide().addClass("toggle_content");
   if(!$('.toggle:first').hasClass('init_hide')) $('.toggle:first').toggleClass("up").toggleClass("down").next().show();

   ($("label.autopopulate").length > 0) ? autopopulateLabels() : "";
});
//---------------------------------------
function _debug ($string) {
   try {
      console.log($string);
   } catch (err) { 
      //$("#footer").append($string + "<div class='hr'><hr /></div>"); 
   }
}
//---------------------------------------
function autopopulateLabels() {
   $.each($("label.autopopulate"), function(){
      //_debug("VAL: " + ($(this).next("input[type='text']").val()));
      if ($(this).next("input[type='text']").val() == "") {
         $(this).next("input[type='text']").attr("value", $(this).html());
      }
      $(this).next("input[type='text']").attr("title", $(this).html());
      $(this).hide();
      //REMOVE populated label on a focus
      $(this).next("input[type='text']").focus(function () {
         // If value and title are equal on focus, clear value
			if (this.value == this.title) {
				this.value = '';
				this.select(); // Make input caret visible in IE
			}
      });
      //ADDED it back if field is empty
      $(this).next("input[type='text']").blur(function () {
         if (!this.value.length) { this.value = this.title; }
      });
   });
}
//---------------------------------------
/*
 * Url preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
this.screenshotPreview = function(){	
   /* CONFIG */
   xOffset = 0;
   yOffset = 10;
   // these 2 variable determine popup's distance from the cursor
   // you might want to adjust to get the right result
   /* END CONFIG */
   $("a[href*='greenvillehd.com']").hover(function(e){
      $(this).removeAttr('title');
      $("#screenshot").remove();

      if($(this).hasClass("no-preview-img")) return;

      var img_dir = "/images/_thumbs/";
      var href = this.href.replace("http://www.greenvillehd.com/","");
      href = ReplaceAll(href,"/","^");
      href = href.substring(0,href.length-1);
      var img = img_dir+href+".jpg";
      var link = this;
      //_debug(img);

      if(!$(this).attr("rel"))
      {
         $.ajax({ 
            url: img, 
            context: document.body, 
            success: function(){
               
               $(link).attr("rel",img);
               display_img(link,img,xOffset,yOffset,e);
               //_debug("IMAGE FOUND - Show Screenshot");
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
               //_debug("IMAGE NOT FOUND");
               $(this).addClass("no-preview-img");
            }
         });
      } else {
         display_img(link,this.rel,xOffset,yOffset,e);
      }
   }
   ,function(){
      $(this).removeAttr('title');
      $("#screenshot").remove();
   });

   $("a[href*='greenvillehd.com']").mousemove(function(e){
     $("#screenshot")
         .css("top",(e.pageY - xOffset) + "px")
         .css("left",(e.pageX + yOffset) + "px");
   });
};
//---------------------------------------
function display_img(target,img,xOffset,yOffset,e)
{
   $(target).removeAttr('title');
   $("body").append("<p id='screenshot'><span class='img'><img src='"+ img +"' alt='url preview' /></span></p>");
   $("#screenshot")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px")
      .fadeIn("fast")
}
//---------------------------------------
//Find and replace a string in another string, with optional case-sensitivity.
function ReplaceAll( inText, inFindStr, inReplStr, inCaseSensitive ) {
   //inText is the text in which to do the search;
   //inFindStr is the string to find;
   //inReplStr is the string to substitute into inText in place of inFindStr; and
   //inCaseSensitive is a boolean value (defaults to false).
   var searchFrom = 0;
   var offset = 0;
   var outText = "";
   var searchText = "";
   if ( inCaseSensitive == null ) {
      inCaseSensitive = false;
   }
   if ( inCaseSensitive ) {
      searchText = inText.toLowerCase();
      inFindStr = inFindStr.toLowerCase();
   } else {
      searchText = inText;
   }
   offset = searchText.indexOf( inFindStr, searchFrom );
   while ( offset != -1 ) {
      outText += inText.substring( searchFrom, offset );
      outText += inReplStr;
      searchFrom = offset + inFindStr.length;
      offset = searchText.indexOf( inFindStr, searchFrom );
   }
   outText += inText.substring( searchFrom, inText.length );
   
   return ( outText );
};
