/**
* Javascript for Ajax Search
*/

jQuery(document).ready(function() {
    jQuery('#searchResults ul li a').hover(function() {
        jQuery(this).addClass('displayIcon');
    }, function() {
        jQuery(this).removeClass('displayIcon');

    });

    $('a[href^=/media/channels/]').live('click', function(e) {
        var r = new RegExp("\/media\/channels\/(.*)\/(.*)\/.*");
        var matches = r.exec(this.href);
        if (matches && matches.length == 3) {
            google.trackEvent('Media_Channel_' + matches[1], 'Play', matches[2]);
        }
    });

    $('a[href^=/media/videos/]').live('click', function(e) {
        var r = new RegExp("\/media\/videos\/(.*)");
        
        var matches = r.exec(this.href);
        if (matches && matches.length == 2) {
            google.trackEvent('Media_Videos', 'Play', matches[1]);
        }
    });
});

//this variable so we can change the divReplaceID dinamically
var divReplaceID = "searchResults";

//FOR ON KEY PRESS...
function searchMediaOnKeyPress(e,sInput,sUrl,sReplaceID)
{
     var evt = e ? e : window.event;
     if (evt.keyCode == 13)
     {
         //do search
         searchMediaByText(sInput.value,'1',sUrl,sReplaceID);
         return false;
     }
}

//search general text
function searchMediaByText(searchText,nPageNo,searchUrl,sReplaceID)
{
     var qs = "type=searchByText&text="+ URLEncode(searchText) +"&page=" + nPageNo;
     searchMedia(searchUrl,qs,sReplaceID);
}

//search based on category
function searchMedia(searchUrl, strQueryString, sReplaceID)
{
     divReplaceID = sReplaceID;
     
     if (window.jQuery)
     {
         //Show the loading spinner
         jQuery("#" + sReplaceID).html("<div style='position:relative; z-index:999; background-color:#fff; margin:20px;'><img src='/global/images/spinner_white.gif' />&nbsp;Loading...</div>" + jQuery("#" + sReplaceID).html());
     
         jQuery.ajax({
                type: "GET",
                url: searchUrl + (strQueryString != "" ? "?" + strQueryString : ""),
                success: showMediaResults,
                failure: showMediaErrors
            });
     }
}



//show result
function showMediaResults(o)
{
    var resultObj = jQuery("#" + divReplaceID);
    if (resultObj)
    {
        resultObj.html(o)

        //add this if the source is change
        jQuery(document).ready(function() { media.init(); } );

    		
    		

        //setupLightbox();
    }
}

//show errors
function showMediaErrors(o)
{
    var resultObj = jQuery("#" + divReplaceID);
    if (resultObj)
    {
        resultObj.html("<div style='color:red;'>Cannot search video based on that category.</div>");
    }
}



function doExplore(shortName, aTag, limitTo){

      jQuery("#" + divReplaceID).css("min-height", "500px");

      jQuery("#" + divReplaceID).html("<div id='explorer'></div>");
             
      if(!limitTo)limitTo="";  
    

      if (location.host.split('.gov.au').length > 1)
      {
        var crossDomainUrl = 'http://content.defencejobs.gov.au/crossdomain.xml';
      }
      else
      {
        var crossDomainUrl = 'http://content.defencejobs2.visualjazz.com.au/crossdomain.xml';
      }


      var flashvars = {shortname: shortName, limitTo : limitTo, policyFile : crossDomainUrl };
      var params = {};
      var attributes = {
        wmode: "opaque",
        menu: "false",
        scale: "noscale",
        allowFullscreen: "true",
        allowScriptAccess: "always"
      };

      swfobject.embedSWF("/media/controls/explorer/explorer.swf", "explorer", "938", "500", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
      
      if(aTag)
      {
          jQuery("a[rel], a[class*=on]").removeClass("on");
          jQuery(aTag).addClass("on");
      }
}


///explorePrettyPhoto('/content/mediaCentre/photos/OperatorBearerSystems.jpg');

function explorePrettyPhoto(imgPath){
  if(jQuery('#explorePrettyPhotoDiv')){
    jQuery('#explorePrettyPhotoDiv').remove()
  }
  
  jQuery('#searchResults').append("<div style='display:none;' id='explorePrettyPhotoDiv'><a id='explorerClick' href='" + imgPath + "' rel='prettyPhoto[gallery1]'>link</a></div>");
  
  // media.init();
    
    jQuery("#explorerClick").prettyPhoto
        ({
            animationSpeed: 'normal', /* fast/slow/normal */    
            padding: 40, /* padding for each side of the picture */
            opacity: 0.50, /* Value betwee 0 and 1 */
            showTitle: false, /* true/false */
            allowresize: true /* true/false */
        }); 
    
    jQuery('#explorerClick').click();

}



