var currentHost = window.location.host+'/video-player';

function getVids() {

  if ((typeof playlist != 'undefined') && (playlist != '')) {
    var urlVars = '?feedURL='+playlist;
  } else {
    var urlVars = '';
  }
  
  // As a default the Uniform Width takes Precedence. If you are using a horzontal layout 
  //for the thumbnails, declare this and set to false before calling the getVids() function
  if (typeof widthTakesPrecedence == 'undefined') {
    widthTakesPrecedence = true; 
  }
  
  if ((typeof uniformWidth == 'undefined') || (uniformWidth == '')) {
    uniformWidth = '';
  } else {
    if (!widthTakesPrecedence) {
      uniformWidth = ' width="'+uniformWidth+'" ';
    }
  }
  
  if ((typeof unformHeight == 'undefined') || (unformHeight == '')) {
    unformHeight = '';
  } else {
    if (widthTakesPrecedence) {
      unformHeight = ' height="'+unformHeight+'" ';
    }
  }
  
  if (widthTakesPrecedence) {
    var dimension = unformHeight;
  } else {
    var dimension = uniformWidth;
  }
  
  if ((typeof vidLimit == 'undefined') || (vidLimit == '')) {
    vidLimit = 0;
  }
  
  if (typeof gallery == 'undefined') {
    gallery = false;
  }
  
  if (typeof gallery == 'undefined') {
    gallery = false;
  }
  
  if ((typeof moreVidsLinkText == 'undefined') || (moreVidsLinkText == '')) {
    moreVidsLinkText = "More Videos";
  }
  if ((typeof moreVidsLinkURL == 'undefined') || (moreVidsLinkURL == '')) {
    moreVidsLinkURL = false;
  }

  jQuery(document).ready(function(){

		//determine what browser
		inThePresenseOfSuck = false;
		jQuery.each(jQuery.browser, function(browser, val) {
			//iterate through browser types, if IE is used, flag inThePresenseOfSuck as true;
			if (browser == 'msie' && val == true) {
			  inThePresenseOfSuck = true;
			}
		});
			
    //populate the holder div
    jQuery('#holder').load("http://"+currentHost+"/get-vids.php"+urlVars, function() {
      if (jQuery('#holder').html() == 'FALSE') {
        jQuery('#videoThumbs').html('<p>No Feed Provided. Check Feed</p>');
      } else {
        //modify the all of the players' embed code to share width and height of containing element
        jQuery('.vidItem .vidEmbed embed').attr('width', parseInt( jQuery("#playerContainer").css('width') ));
        jQuery('.vidItem .vidEmbed embed').attr('height', parseInt( jQuery("#playerContainer").css('height') ));
        //Get the fist item's embed code and place it in playerContainter
        jQuery("#playerContainer").html( jQuery("#holder > div:first .vidEmbed").html() );
        
        //Traverse the holder to get the thumbs and populate the video thumbs area
        if (!vidLimit || vidLimit > jQuery('.vidItem').length) {
          vidLimit = jQuery('.vidItem').length;
        }
        if (vidLimit > 0) {
          for (x = 0; x <= vidLimit-1; x++) {
            thumbURL = jQuery('#holder #thumb-'+ x +' img').attr('src');//get image src
            thumbTitle = jQuery('#title-'+ x).html();//get Title
            description = jQuery('#description-'+ x).html();//get Title
            divMarkup = '<img id="'+x+'" class="thumbImg"' + dimension + 'src="'+thumbURL+'" alt="" title="'+thumbTitle+'" />';
            if (gallery) { //set up for gallery or just dislay thumbnail.
              divMarkup = '<div class="galleryItem">'+divMarkup+'<div class="textContainer"><h4 id="'+x+'">'+thumbTitle+'</h4>'+description+'</div></div>';
            }
            jQuery('#videoThumbs').append(divMarkup);//append the image to #videoThumbs
            
            //size the thumbnails to a uniform width or height
            if (widthTakesPrecedence) {
              var imgWidth = ( jQuery('#'+x).width() );
              //uniformWidth = parseInt(uniformWidth);
              if (imgWidth != uniformWidth) {
                //remove height attribute
                jQuery('#'+x).removeAttr('height');
                jQuery('#'+x).attr('width', uniformWidth);
              }

            } else {
              var imgHeight = ( jQuery('#'+x).height() );
              if (imgHeight != unformHeight) {
                //remove height attribute
                jQuery('#'+x).removeAttr('width');
                jQuery('#'+x).attr('height', unformHeight);
              }
            }            
          }
          
          // Add More Videos link
          if (moreVidsLinkURL) {
            jQuery('#videoThumbs').append('<a class="moreVidsLink" href="'+moreVidsLinkURL+'">'+moreVidsLinkText+'</a>');
          }
          
        }

        jQuery('#videoThumbs img, .galleryItem h4').click(function () {
          // get id of image that was clicked
          id = jQuery(this).attr('id');
          //set the item to auto-play (IE bug causes errors: check against that)
          if (!inThePresenseOfSuck) { 
            // When IE sets attributes it removes surrounding quotes. This is not according to standards and does not play well with blip. Yet another reason why IE sucks the nastiest balls.
            jQuery('#player-'+id+' embed').attr('src', (jQuery('#player-'+id+' embed').attr('src')+'/?autostart=true'));
          }
          jQuery('#playerContainer').html( '<a name="video"></a>'+jQuery('#holder div div.vidEmbed').eq(id).html() );
          //take them to the top
          if (gallery) {
            window.location = "#video";
          }
        });
      }
    });
  });
}