// Collect the current user agent
var thisAgent = navigator.userAgent.toLowerCase();
// Detect if the user agent is iPod, iPhone, or iPad
var iPod = (thisAgent.indexOf('ipod') != -1) ? true : false;
var iPhone = (thisAgent.indexOf('iphone') != -1) ? true : false;
var iPad = (thisAgent.indexOf('ipad') != -1) ? true : false;
// Define whether a mobile device is being used
var isMobile = (iPod || iPhone || iPad) ? true : false;

// Define the click-events for the videos
$(document).ready(function(){
  // Change any pretty photo links to lightboxes
  if (jQuery().colorbox){
    $("*[data-colorbox]").each(function(){
      // Prevent the default click action
      $(this).click(function(e){e.preventDefault();});
      // Check if this is an iframe or inline element
      var isIframe = $(this).attr('data-colorbox') == 'iframe' ? true : false;
      var isInline = $(this).attr('data-colorbox') == 'inline' ? true : false;
      // Attempt to collect the HREF from the element
      var thisHref = $(this).attr('href') ? $(this).attr('href') : false;
      // Attempt to collect settings if any are defined
      var customSettings = $(this).attr('data-settings') ? $(this).attr('data-settings') : false;
      // Define the allowable settings editorSettings
      var colorboxSettings = {boxHref:thisHref,boxClose:'false',boxWidth:(isIframe || isInline ? 600 : false),boxHeight:(isIframe || isInline ? 550 : false)};
      // Attempt to parse the custom settings if present
      plutocms_parse_customsettings(customSettings, colorboxSettings);
      // If the HREF is an actual URL, attach the event
      if (colorboxSettings.boxHref != false){
        // If this is NOT a mobile device
        if (!isMobile){
          // Attach the appropriate colorbox event based on type
          if (isIframe){
            $(this).colorbox({overlayClose:false,opacity:0.5,current:'{current} / {total}',href:colorboxSettings.boxHref,iframe:true,innerWidth:colorboxSettings.boxWidth,innerHeight:colorboxSettings.boxHeight,onClosed:(colorboxSettings.boxClose != 'false' ? function(){window.location=colorboxSettings.boxClose+window.location.hash;} : false)});
            } else if(isInline){
            $(this).colorbox({overlayClose:false,opacity:0.5,current:'{current} / {total}',href:colorboxSettings.boxHref,inline:true,innerWidth:colorboxSettings.boxWidth,innerHeight:colorboxSettings.boxHeight,onClosed:(colorboxSettings.boxClose != 'false' ? function(){window.location=colorboxSettings.boxClose+window.location.hash;} : false)});
            } else{
            $(this).colorbox({opacity:0.5,current:'{current} / {total}',href:colorboxSettings.boxHref,innerWidth:colorboxSettings.boxWidth,innerHeight:colorboxSettings.boxHeight,onClosed:(colorboxSettings.boxClose != 'false' ? function(){window.location=colorboxSettings.boxClose+window.location.hash;} : false)});
            }
          } else {
            $(this).click(function(e){ window.location = colorboxSettings.boxHref; });
          }
        }
      });
    }
});

// Define a function for parsing custom settings out of an HTML attribute
function plutocms_parse_customsettings(customSettings, extensionSettings){
  if (customSettings.length){
    var settingPairs = customSettings.split('|');
    for (var i = 0; i < settingPairs.length; i++){
      var temp = settingPairs[i].match(/([-_a-z0-9]+)=(.*)/i);
      if (!temp){ continue; }
      var thisKey = temp[1] ? temp[1] : false;
      var thisValue = temp[2] ? temp[2] : false;
      if (thisKey == false && thisValue == false){
        continue;
        }else{
        extensionSettings[thisKey] = thisValue;
        }
      }
    } 
}
