// Take care of the Clickpass panel. Catches messages from Clickpass Iframes

function ClickpassPanel(div_id, options ){
  if(!options){
    options = {}
  }
  var clickpass_div_id = div_id;
  var CLICKPASS_DOMAIN = options.clickpass_domain ? options.clickpass_domain : "http://www.clickpass.com"
  if(location.href.search("https://") == 0 && CLICKPASS_DOMAIN.search("https://") != 0){
    CLICKPASS_DOMAIN = "https://"+CLICKPASS_DOMAIN.substring(7);
  }
  var clickpass_popup_id = options.clickpass_popup_id;
  var previousHeight = null;
  

  // make the iframe size smaller or larger in order to fit the additional openids
  var resizeIframe = function (makeBig){
    if (makeBig) {
      previousHeight = iframe.height;
      iframe.height = (parseInt(iframe.height)+222)+"px";
    }
    else {
      if (previousHeight){
        iframe.height = previousHeight;
      }
      else {
        iframe.height = "18px";
      }
    }
  }

  // find the clickpass iframe from the given div
  var getClickpassIframe = function(div_id) {
    div_element = document.getElementById(div_id);
    all_iframes = div_element.getElementsByTagName('iframe');
    for (var i = 0; i < all_iframes.length; i++) {
      if (all_iframes[i].src.search ( 'clickpass.com' ) != -1){
        return all_iframes[i];
      }
    }
  }

  // get the site params from a url
  var getSiteParams = function(site_url) {
    return_site_params = {};
    // site_key
    return_site_params.site_key = getParamFromUrl(site_url, 'site_key');
    if (!return_site_params.site_key) {
      // try to suck it from the url
      var site_key_rexp = /\/\w+((\?)|($))/
      var site_key_end_rexp = /\?|$/
      return_site_params.site_key = (site_url.substring(site_url.search(site_key_rexp)+1, site_url.search(site_key_end_rexp) ));
    }

    // parameter1
    return_site_params.parameter1 = getParamFromUrl(site_url, 'parameter1');

    //override_openid_callback_url
    return_site_params.override_openid_callback_url = getParamFromUrl(site_url,'override_openid_callback_url');

    return return_site_params;
  }

  // helper function to suck out a parameter from a url
  var getParamFromUrl = function(url, param_name){
    param_name = param_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+param_name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( url );
    if( results == null )
      return null;
    else
      return results[1];
  }

  // helper function that adds parent_url to an iframe with given id
  this.concatParentUrl = function(iframe) {
    var delim = (iframe.src.indexOf('?')!=-1) ? '&' : '?';

    iframe.src += delim+"parent_url="+encodeURIComponent(parentUrl());
  }

  // helper function to get the parent url from the location.href
  var parentUrl = function() {
    return location.href.replace(/#.*$/, '');
  }

  // suck out the parent url from the iframe
  var getParamsUrl = function(action) {
  alert(parentUrl());
    var return_src = CLICKPASS_DOMAIN+"/embedded_interfaces/"+action;
    return_src += "?site_key="+encodeURIComponent(siteParams.site_key);
    return_src += "&parent_url="+encodeURIComponent(parentUrl());
    if(siteParams.parameter1){
      return_src += "&parameter1="+encodeURIComponent(siteParams.parameter1);
    }
    if(siteParams.override_openid_callback_url) {
      return_src += "&override_openid_callback_url="+encodeURIComponent(siteParams.override_openid_callback_url); 
    }

    return return_src;
  }
  
  var fragmentWatcher = function(){
    if(window.parent.location.hash == '#cp_make_big') {
      window.parent.location.hash ="cp"
      resizeIframe(true);
    }
    else if(window.parent.location.hash == '#cp_make_small') {
      window.parent.location.hash ="cp"
      resizeIframe(false);
    }
    setTimeout( fragmentWatcher, 50);
  }

  // INITIALISER INSTRUCTIONS
  // suck out the iframe
  if(div_id) { 
    var iframe  = getClickpassIframe(div_id);
    this.concatParentUrl(iframe);

    //suck out the parameters from the iframe url
    var siteParams = getSiteParams(iframe.src);
  }

  // watch the fragment for any cross domain clickpass instructions
  fragmentWatcher();

  var flipDynamic = function ( showStatic ) {
    var clickpassButton = document.getElementById('clickpass_button');
    if (!clickpassButton) return;

    clickpassButton.className = showStatic ? "static" : "dynamic";
  }

  flipDynamic(true);
  setTimeout(function(){flipDynamic(false);}, 2000);
}

window.scriptLoaded = true;
