// Media Block callback script
window['_lpmb'] = function (json) {
  if (json && json.collection) {
    var mediaCollection = json.collection.medias;
    
    // Truncate the media collection the first few images to display
    var mediaCollectionToDisplay = new Array();
    $(mediaCollection).each(function () {
      if (mediaCollectionToDisplay.length < 4) {
        var mediaType = this['media_type'];
        if (mediaType == 'LPI-IMAGE') {
          mediaCollectionToDisplay.push(this);
        }
      }
    });
    

    $.each(mediaCollectionToDisplay, function(idx, mediaToDisplay) {
      var id = mediaToDisplay['id'];
      var title = mediaToDisplay['title'];          
      var src = mediaToDisplay['resource_location'];
      var mediaReference = mediaToDisplay['reference'];
      
      var refParts = mediaReference.split('-', 2);
      
      var img = new Image();
      img.id = id;
      img.title = title;
      img.src = mediaImgUrl + '/' + refParts[0] + '/' + mediaReference + '/64x48.jpg';
      
      var imageSource = mediaImgUrl + '/' + refParts[0] + '/' + mediaReference + '/64x48.jpg';

      var mediaAnchorLink = 'photo-' + mediaReference;
      var imageLink = destinationUrl + "&mediaAnchorLink=" + mediaAnchorLink + '#' + mediaAnchorLink;
      
      $('#mediaBlockThumbs')
        .append($('<a href="' + imageLink + '"><img src="' + imageSource + '"/></a>'))
        .show();
      
      if (idx == 0) {
        $('#mediaBlockLink').attr('href', imageLink);
      }
    });
    $('#mediaBlockThumbs').show();
    $('#mediaBlock').show();                
  }
}

$(document).bind('maps.mediaBlock.ready', function() {
  $('#mediaBlockThumbs').empty();
  var actualMediaServerUrl = mediaSvcUrl.replace(/\[DESTINATION_ID\]/, destinationsId);
	$.ajax({
		type: 'GET',
		url: actualMediaServerUrl,
		dataType: 'script',
		cache: true
	});
});

// ShopPod callback script
$(document).bind('maps.shopPod.ready', function() { 
  var shopUrlForDestination = shopUrl; 
  if (ethylCobjId != '-1') {
    shopUrlForDestination = shopUrlForDestination + "?ethylIds=" + ethylCobjId + "&lpaffil=lpmaps-shoplinks";
  }
  $('#shopPod').html("<iframe id='shopFrame' height='230' width='317' scrolling='no' frameborder='0' src='" + shopUrlForDestination + "'/>");
});

// The main "page ready" script
$(document).ready(function() {
	
  	//Attach the search form functionality	
	$("#search_form").submit(function(){
							var searchVal = $.trim($("#search_form_text").val());
							var defaultSearchVal = document.getElementById("search_form_text").defaultValue;
							
							if(searchVal == "" || searchVal == defaultSearchVal)
							{
								alert("Please enter a search term");
								return false;
							}
							else
							{
								return true;
							}
							});
	
	$("#search_form_text").focus(function(){
							if($(this).val() == this.defaultValue)
							{
							$(this).val("");  
							}
							}).blur(function(){
							if($.trim($(this).val()) == "")
							{
							$(this).val(this.defaultValue);  
							}
							});
	
});
