var customer_id = 4 ;
var content_length = 30;
var img_url_root = 'http://images.ropads.net/images/highlander';
var ws_url_root = 'http://admin.ropads.net';

jQuery("head").append('<link rel="stylesheet" href="' + ws_url_root + '/adwidget.css" type="text/css" />')
jQuery("head").append('<link rel="stylesheet" href="' + ws_url_root + '/embed/theme.css?customer_id=' + customer_id + '" type="text/css" />')


//Plugin portion; binds elements as embedded eds
jQuery.fn.CIEmbedAds = function(ad_count, category) {
    return this.each( function() {
      if (typeof(ad_count) != "number" || ad_count < 1 ) {
          ad_count = 1;
      }
      if (typeof(category) != "string") {
        category = "";
      }
      var ad_box = jQuery(this);
      url = ws_url_root
            + "/embed/get_ad_brief_random?customer_id="
            + customer_id
            + "&count="
            + ad_count
            + "&category="
            + category
            + "&callback=?"
      jQuery.getJSON(url, function(r) {
          ad_box.replaceWith(r.html);
      });
    });
}

// Call popup on click
jQuery(".SeeingInteractive_ad_row").live("click", function() {
    id = jQuery(this).children(".SeeingInteractive_id_number").attr("value");
    url = ws_url_root
      + "/embed/get_ad_full/" + id + "/?"
      + "&callback=?";
    showPopup(url, img_url_root);
});

// Widget behavior
jQuery(".SeeingInteractive_ad_row").live("mousemove", function(e) {
    jQuery(this).children(".SeeingInteractive_thumbnail").show();
    jQuery(this).children(".SeeingInteractive_thumbnail").css("top", e.pageY + 15);
    jQuery(this).children(".SeeingInteractive_thumbnail").css("left", e.pageX + 15);
});

jQuery(".SeeingInteractive_ad_row").live("mouseout", function() {
    jQuery(this).children(".SeeingInteractive_thumbnail").hide();
});

//Popup stuff
jQuery(document).ready(function() {
  if (jQuery(".SeeingInteractive_ad_popup").length == 0) {
    jQuery("body").append('<div class="SeeingInteractive_ad_popup"></div>');
    jQuery('body').append('<div class="SeeingInteractive_page_overlay"></div>');
  }
  jQuery(".SeeingInteractive_close_ad_popup").live("click", function() {
    jQuery(".SeeingInteractive_ad_popup").hide();
    jQuery('.SeeingInteractive_page_overlay').hide();
  });
});

function showPopup(url, img_url_root) {
    jQuery.getJSON(url , function(r) {
        jQuery(".SeeingInteractive_ad_popup").empty();
        jQuery(".SeeingInteractive_ad_popup").css("left", "-9999");
        jQuery(".SeeingInteractive_ad_popup").show();
        jQuery('.SeeingInteractive_page_overlay').show();
        setOverlaySize();
        jQuery(".SeeingInteractive_ad_popup").append('<div class="SeeingInteractive_close_ad_popup">Close</div>');
        jQuery(".SeeingInteractive_ad_popup").append('<img class="SeeingInteractive_img_ad_popup" src="'+ img_url_root + "/" + r.normal_image_url + '" />');
        jQuery(".SeeingInteractive_img_ad_popup").load( function() {
            var left = ( jQuery().width("html") - jQuery(".SeeingInteractive_ad_popup").width() ) / 2;
            jQuery(".SeeingInteractive_ad_popup").css("top", jQuery(window).scrollTop() + 30 );
            jQuery(".SeeingInteractive_ad_popup").css("left", left);
            setOverlaySize();
        });
    });
}

function setOverlaySize() {
  var height = jQuery('body').height();
  var ph = jQuery('.SeeingInteractive_ad_popup').height() + jQuery('.SeeingInteractive_ad_popup').position().top;
  var wh = jQuery(window).height();
  if (ph > height) {
    height = ph;
  }
  if (wh > height) {
    height = wh;
  }
  width = jQuery(window).width();
  jQuery('.SeeingInteractive_page_overlay').css('width', width + 100);
  jQuery('.SeeingInteractive_page_overlay').css('height', height + 10);
}