/** popIn API  start **/

var PopInApi = {};

PopInApi.init = function(){
	PopInApi._param = PopInApi.loadParams();    
}

PopInApi.getParam = function(param){
	return PopInApi._param[param];
}  

PopInApi.loadParams = function() {
  var param = new Object();
  var qs  = location.search; 

  if (qs && qs.length > 2)
  {
    qs = qs.slice(1);
    qs1 = qs.split('&');
    for (var i = 0 ; i < qs1.length ; i++)
    {
      var tmp = qs1[i].split('=');
      if( tmp[1] != "")
        param[tmp[0]] = tmp[1];
    }
  }
  return param;
}



/**
  Generic Execute
**/
PopInApi.execute = function(command, params) {
 
  param = PopInApi.loadParams();
  
  if(typeof popIn == "object"){	//For Rainbow
  	return PopInApi._execute_enabler(command, params);
  }else if(param['type'] == "enabler"){ //For Rainbow Flash
  	return PopInApi._execute_flash(command, params);
  }else if (window.popInPlugin){ //For Safari
  	return PopInApi._execute_safariplugin(command, params);
  }else if (window.ActiveXObject){	//For IE
    return PopInApi._execute_ieplugin(command, params);
  }else if ("createEvent" in document){ //For Firefox
    return PopInApi._execute_firefoxplugin(command, params);
  }
  else {
    throw "POPIN-NOT-FOUND";
  }
  
};


PopInApi._getObject = function(name){
  if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[name];
  }
  else {
    return document[name];
  }
}

/**
  Firefox Plugin
 **/
PopInApi._execute_firefoxplugin = function(command, params) {
  var element = document.createElement("PopInElement");
  element.setAttribute("command", command);
  		    
  for (var i in params) {
  	element.setAttribute(i, params[i]);
  }
  document.documentElement.appendChild(element);
  var evt = document.createEvent("Events");
  evt.initEvent("PopInServiceCommand", true, false);
  element.dispatchEvent(evt);
  if (element.getAttribute("error"))
  {
    throw element.getAttribute("error");
  }  
  else
  {
    var attributes = element.attributes;
    var result = {};
    for (var i = 0; i < attributes.length; i++)
    {
      var value = attributes.item(i).nodeValue;
      if(value[0] == '[' || value[0] == '{') value = eval(value);      
      result[attributes.item(i).localName] = value;       
    }
    return result;
  }
};

/**
  Enabler
 **/
PopInApi._execute_enabler = function(command, params) {
  if (typeof params == "string") eval("params=" + params+";");
  var element = document.createElement("PopInElement");
  element.setAttribute("command", command);		    
  if (params) for (var i in params) element.setAttribute(i, params[i]);
  PopIn.ServicesHelper.handleEvent({type:"PopInServiceCommand", target:element});

  var attributes = element.attributes;
  var result = {};
  for (var i = 0; i < attributes.length; i++)
  {
    var value = attributes[i].nodeValue;
    if (value != null && typeof value != "undefined" && value.length > 0)
    {
      if(value[0] == '[' || value[0] == '{') eval("value = " + value + ";");      
      result[attributes.item(i).nodeName] = value;       
    }
  }
  return result;  
};

/**
  IE Plugin
**/
PopInApi._execute_ieplugin = function(command, params) {
	var result = {};
	
try{
		
		var obj = new ActiveXObject("PopIn.PopInButton");	
		var data = obj.callBHOProcEx("PopInServicesIEHelper","get-version******");
		
		if(data.indexOf("[") == 0){
			result["result"] = eval("data = [" + data.substring(1,data.length-1) +"];");
		}else if(data.indexOf("{") == 0){
			result["result"] = eval("data = {\"" + data.substring(1,data.length-1) +"\"};");
		}else{
			result["result"] = data;
		}
 	  
		if(typeof params == "object"){
			var tempParams = "";
			for (var i in params){
				if(String(params[i]).indexOf('[') < 0 && String(params[i]).indexOf('{') < 0){
			 		tempParams += i + ":'" + params[i] + "',";
			 	}else{
			 		tempParams += i + ":" + params[i] + ",";
			 	}
			 	
			};
			params = tempParams.substring(0,tempParams.length - 1);
		}
		
 	  	var data = obj.callBHOProcEx("PopInServicesIEHelper",command +"******"+params);
	
		
		if(data.indexOf("[") == 0){
			result["result"] = eval("data = [" + data.substring(1,data.length-1) +"];");
		}else if(data.indexOf("{") == 0){
			result["result"] = eval("data = {\"" + data.substring(1,data.length-1) +"\"};");
		}else{
			result["result"] = data;
		} 
 	 
	       
	  }catch(e){
	   result["result"] = "undefined";
	  }
	  
	  return result;
};

/**
  Safari Plugin
**/
PopInApi._execute_safariplugin = function(command, params) {
	var result = {};
	
	try{

		if(typeof params == "object"){
			var tempParams = "";
			for (var i in params){
				if(String(params[i]).indexOf('[') < 0 && String(params[i]).indexOf('{') < 0){
			 		tempParams += i + ":'" + params[i] + "',";
			 	}else{
			 		tempParams += i + ":" + params[i] + ",";
			 	}
			 	
			};
			params = tempParams.substring(0,tempParams.length - 1);
		}

		
		var data = popInPlugin.popInAPI_param1_param2_("PopIn.ServicesHelper.safariHelper",escape(command +"******"+params),""); 
		
		if(data.indexOf("[") == 0){
			result["result"] = eval("data = [" + data.substring(1,data.length-1) +"];");
		}else if(data.indexOf("{") == 0){
			result["result"] = eval("data = {\"" + data.substring(1,data.length-1) +"\"};");
		}else{
			result["result"] = data;
		}
		
		
	    
	}catch(e){
	  result["result"] = "undefined";
	}
	
	return result;
	  
};

/**
 Flash
**/
PopInApi._execute_flash = function(command, params){
  if (command != "display-object")
  {
    throw "Command not supported [" + command + "]";
  }
  var data = PopInApi.serialize(params);
  var movie = PopInApi._getObject("send");
  movie.sendText(PopInApi.getParam("connectionID"),data);  
};

PopInApi.serialize = function(obj){
  if(!obj) return "null";
  if (typeof obj == "number") return obj;
  else if (typeof obj == "string") return "\""+obj+"\"";
  else if (typeof obj == "boolean") return obj ? "true" : "false";
  else if (obj.length >= 0) {
    var rv = "";
    for (var i = 0; i < obj.length; i++) rv += (rv.length==0 ? "" : ",") + PopInApi.serialize(obj[i]);
    return "[" + rv + "]";
  }  
  else if (typeof obj == "object"){
    var rv = "";
    for (var i in obj) rv += (rv.length==0 ? "" : ",") + "\"" + i + "\": " + PopInApi.serialize(obj[i]);
    return "{" + rv + "}";
  }
  else return "?";
};

/** popIn API End **/


//popIn Server Setting
//var POPIN_SERVER = "http://133.11.169.233/popIn_Core/deploy/";
var POPIN_SERVER = "./js/"; 
//var POPIN_SERVER = "http://e.popin.cc/";


//popIn Config Data
var popInConfigData = {"popInConfig":{

//POPIN_CONFIG:""
"service":[{"enabled":true,"serviceId":1,"serviceVer":1,"isDIY":false,"hasPara":true,"serviceName":"Google","serviceIcon":"http://google.co.jp/favicon.ico","serviceUrl":"http://popin.cc/en/services/search/search.html?qstring=","serviceOriUrl":"http://www.google.com/search?hl=en&q={qstring}","displayOrder":0,"isSearch":true,"optionName":"","optionData":"","addedDay":"","inputModes":{"selectMode":{"type":"popInWin","width":420,"height":350,"isDraggable":false,"isResizable":"true","isPopin":true},"shortcutMode":{"screenx":10,"screeny":10,"width":420,"height":350}}},{"enabled":true,"serviceId":2,"serviceVer":1,"isDIY":false,"hasPara":true,"serviceName":"Google Maps","serviceIcon":"http://maps.google.com/favicon.ico","optionName":"","optionData":"","serviceUrl":"http://popin.cc/ja/services/map/map.html?q=","serviceOriUrl":"http://maps.google.co.jp/maps?hl=ja&q={qstring}","displayOrder":2,"isSearch":true,"addedDay":"","inputModes":{"selectMode":{"type":"popInWin","width":700,"height":450,"offsetY":0,"isDraggable":false,"isResizable":"true","isPopin":true},"shortcutMode":{"screenx":10,"screeny":10,"width":700,"height":450,"offsetY":0}}},{"enabled":true,"serviceId":3,"serviceVer":1,"isDIY":false,"hasPara":true,"serviceName":"Wikipedia","serviceIcon":"http://ja.wikipedia.org/favicon.ico","serviceUrl":"http://popin.cc/en/services/wikipedia/index.html?q=","serviceOriUrl":"http://en.wikipedia.org/wiki/{qstring}","displayOrder":1,"isSearch":true,"optionName":"","optionData":"","addedDay":"","inputModes":{"selectMode":{"type":"popInWin","width":420,"height":350,"isDraggable":false,"isResizable":"true","isPopin":true},"shortcutMode":{"screenx":10,"screeny":10,"width":420,"height":350}}},
{serviceId:"6",enabled:true,isNew:false,serviceVer:1,serviceName:"YouTube",isSearch:true,serviceIcon:"http://jp.youtube.com/favicon.ico",serviceUrl:"http://www.popin.cc/en/services/youtube/youtube.html?q=",serviceOriUrl:"http://jp.youtube.com/results?search_query=",displayOrder:6,optionName:"",optionData:"",isDIY:"false",hasPara:"true",inputModes:{selectMode:{type:"popInWin",width:450,height:350,isDraggable:"true",isResizable:"true",isPopin:"true"},shortcutMode:{screenx:10,screeny:10,width:450,height:350}}}],"groups":[],"updateDate":"20090416","isPopinEnabler":true,"enablerID":"enabler92b894b4-805c-bafc-4187-e488b4b13deb","lang":"en","isEnable":"true","isLinkIconEnable":"true","installPage":"http://www.popin.cc/ja/plugin/function.html","helpPage":"http://www.popin.cc/ja/plugin/help.html","updatePage":"http://popin.cc/ja/plugin/update.html","customizePage":"http://www.popin.cc/ja/plugin/setting.html","previewMode":0,"hotKey":2,"isFlashEnable":false,"skinColor":"color_black","rakutenId":"","amazonId":"","arText":"","lastCheckDate":"0","userID":"","isPreviewEnable":"true"


}};


document.write('<span id="popInBlogSWF"></span>');
document.write('<span id="popInSWF"></span>');
/*
  if (document.readyState == "complete")
                {
                         document.writeln('<span id="popInBlogSWF"></span>');
						 document.writeln('<span id="popInSWF"></span>');
                 }
*/
                 
var Popin_LoadModule = function(url)
{ 
  var e = document.createElement("script");
  e.src = url;
  e.charset="UTF-8";  
  document.getElementsByTagName("head")[0].appendChild(e); 
}


function Popin_installedAddon(){
	var version = PopInApi.execute("get-version").result;
	if(typeof version == "undefined" || version == "undefined"){
		return false;	
	}else{
		return true;
	}
}

	

if(Popin_installedAddon()){	
  Popin_LoadModule(POPIN_SERVER + "./popin_rainbow_plugin-min.js");		
}else{
	Popin_LoadModule(POPIN_SERVER + "./popin_plugin_demo-min.js");
}
