

/* net
 *
 *
 */
/*
url-loading object and a request queue built on top of it
*/

/* namespacing object */
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;


/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded';
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
	  this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}


net.ContentLoader.prototype.defaultError=function(){
 // alert("error fetching data!"
//    +"\n\nreadyState:"+this.req.readyState
//    +"\nstatus: "+this.req.status
//    +"\nheaders: "+this.req.getAllResponseHeaders());
}

function getRequests(referrer){
	referrer = referrer.substring(referrer.indexOf("?")+1).split("&");
	var requests = new Array();

	for ( index in referrer )
	{
		var temp = referrer[index].split("=");
		requests[temp[0]]=decodeURI(temp[1]);	
	}
	return requests;
}

function write_ok(){
	writer=null;
}

var count;
function loading_ok(){
	count = this.req.responseText;
//	alert(this.req.responseText);

	var requests = getRequests(document.referrer);
	if( requests["OVKEY"] != undefined ){

		for( index in rotationMap.shop){
			if(requests["OVKEY"].indexOf(rotationMap.shop[index]) > -1) {
				var prams;
				prams = "src=" +  rotationMap.imagelink[index];
				prams += "&title=" + (rotationMap.shop[index]);
				prams +="&count="+count;
				prams +="&referer="+(document.referrer);

				var url = rotationMap.imagelink[index];
//				alert(prams);

				if(!writer) writer= new net.ContentLoader("http://www.siemensworld.com/new/test.php",
					function(){
						//document.write(this.req.responseText);
						location.href = url;
					},
					BuildError, "POST", prams
				);
			}
		}
	}
}


function BuildError(){
//	alert("¿¡·¯°¡ ¹ß»ýÇß½À´Ï´Ù!!!!!!! \n\nreadyState:"+ this.req.readyState + "\nstatus: "+ this.req.status +"\nheaders: "+ this.req.getAllResponseHeaders());
}


/*RotationMap
 *
 *
 */
function RotationMap(){
	this.timer;
	this.oldDate;
	this.newDate;
	this.term = RotationMap.MIN;
	this.pointer;
	
	this.shop = new Array();
	this.screanshotImageFolder="img/";
	this.screanshotImage = new Array();

	this.mapImageFolder="map/";
	this.mapImage = new Array();

	this.imagelink = new Array();

	this.mapCounter=0;
	this.addMap("°­³²","01.gif","01_.gif","http://www.nicesound.co.kr");

}
RotationMap.MIN =1;
RotationMap.HOUR=2;
RotationMap.DAY=3;

RotationMap.prototype.addMap =function(shop,screanshotImage,mapImage,imagelink){
	this.mapCounter++;
	this.shop.push(shop);
	this.screanshotImage.push(screanshotImage)
	this.mapImage.push(mapImage)
	this.imagelink.push(imagelink)
}

RotationMap.prototype.over =function(src)
{
	name = src["src"];
	index = Number(name.slice(name.lastIndexOf("/")+1,name.lastIndexOf(".")));
	mapImage.src = this.mapImageFolder + this.mapImage[index];
	document.getElementById('map').style.visibility='visible';
}

RotationMap.prototype.out = function()
{
	document.getElementById('map').style.visibility='hidden';
}

RotationMap.prototype.checkTimestamp=function()
{
	rotationMap.newDate = new Date();

	if((rotationMap.term == RotationMap.MIN && rotationMap.newDate.getMinutes() != rotationMap.oldDate.getMinutes()) 
	|| (rotationMap.term == RotationMap.HOUR && rotationMap.newDate.getHours() != rotationMap.oldDate.getHours())
	|| (rotationMap.term == RotationMap.DAY && rotationMap.newDate.getDate() != rotationMap.oldDate.getDate())){
		rotationMap.changeImage();
	}
}

RotationMap.prototype.changeImage = function()
{
		this.oldDate = this.newDate;

		var nextPoint;
		var previewImageCounter=0;

		try{
			previewImageCounter +=previewImage_top.length;
		}catch(e){}

		try{
			previewImageCounter +=previewImage_right.length;
		}catch(e){}

		try{
			previewImageCounter +=previewImage_bottom.length;
		}catch(e){}

		try{
			previewImageCounter +=previewImage_left.length;
		}catch(e){}

		var mapLink_top = document.getElementsByName('mapLink_top');	
		for (var i =0,p=this.pointer ;i < previewImage_top.length ; i++,p++)
		{
			if(p >( previewImageCounter )) p =01;

			previewImage_top[i].src=this.screanshotImageFolder+this.screanshotImage[p];
			previewImage_top[i].title=this.shop[p];
			mapLink_top[i].href=this.imagelink[p];

			var oInstance = this; 
			if(mapLink_top[i].addEventListener){
				mapLink_top[i].addEventListener("click",function(event){oInstance.click(event);},false);
			}
			else if(mapLink_top[i].attachEvent){
				mapLink_top[i].attachEvent("onclick",function(event){oInstance.click(event);});
			}
			nextPoint =p;
		}

		var mapLink_right = document.getElementsByName('mapLink_right');	
		for (var i =0,p=nextPoint+1;i < previewImage_right.length ; i++,p++)
		{
			if(p > ( previewImageCounter ) ) p =01;

			previewImage_right[i].src=this.screanshotImageFolder+this.screanshotImage[p];
			previewImage_right[i].title=this.shop[p];
			mapLink_right[i].href=this.imagelink[p];

			var oInstance = this; 
			if(mapLink_right[i].addEventListener){
				mapLink_right[i].addEventListener("click",function(event){oInstance.click(event);},false);
			}
			else if(mapLink_right[i].attachEvent){
				mapLink_right[i].attachEvent("onclick",function(event){oInstance.click(event);});
			}
			nextPoint =p;
		}

		var mapLink_bottom = document.getElementsByName('mapLink_bottom');	
		for (var i =previewImage_bottom.length-1,p=nextPoint+1;i >= 0  ; i--,p++)
		{
			if(p > ( previewImageCounter ) ) p =01;

			previewImage_bottom[i].src=this.screanshotImageFolder+this.screanshotImage[p];
			previewImage_bottom[i].title=this.shop[p];
			mapLink_bottom[i].href=this.imagelink[p];

			var oInstance = this; 
			if(mapLink_bottom[i].addEventListener){
				mapLink_bottom[i].addEventListener("click",function(event){oInstance.click(event);},false);
			}
			else if(mapLink_bottom[i].attachEvent){
				mapLink_bottom[i].attachEvent("onclick",function(event){oInstance.click(event);});
			}
			nextPoint =p;
		}

		try{
			var mapLink_left = document.getElementsByName('mapLink_left');	


			for (var i =previewImage_left.length-1,p=nextPoint+1;i >= 0  ; i--,p++)
			{
				if(p > ( previewImageCounter ) ) p =01;

				previewImage_left[i].src=this.screanshotImageFolder+this.screanshotImage[p];
				previewImage_left[i].title=this.shop[p];
				mapLink_left[i].href=this.imagelink[p];

				var oInstance = this; 
				if(mapLink_left[i].addEventListener){
					mapLink_left[i].addEventListener("click",function(event){oInstance.click(event);},false);
				}
				else if(mapLink_left[i].attachEvent){
					mapLink_left[i].attachEvent("onclick",function(event){oInstance.click(event);});
				}
				nextPoint =p;
			}
		}catch(err){
		}

		this.pointer++;
		if(this.pointer > ( previewImageCounter )) this.pointer =01;
}
RotationMap.prototype.init =  function()
{
	this.oldDate = new Date();	
	switch(this.term)
	{
		case RotationMap.MIN:this.pointer = (Number(this.oldDate.getTime()/60000) % this.mapCounter);
						break;
		case RotationMap.HOUR:this.pointer = (Number(this.oldDate.getTime()/(60000*60)) % this.mapCounter);
						break;
		case RotationMap.DAY:this.pointer = (Number(this.oldDate.getTime()/(60000*60*24)) % this.mapCounter);
						break;
	}
	this.pointer = Math.floor(this.pointer);
	if(this.pointer >this.mapCounter)this.pointer = this.mapCounter;

	this.oldDate.setTime(0);
	this.newDate = new Date();

	this.changeImage();

	var oInstance = this; 
	this.timer = setInterval(function(){oInstance.checkTimestamp()},1000);
}

var writer;
RotationMap.prototype.click =  function(event)
{	
	var prams;

	if(event["currentTarget"]){
		//ÆÄÀÌ¾îÆø½º
		prams = "src=" + event["currentTarget"];

	}else if(event["srcElement"]["parentElement"]){
		/*
		var pp;
		for(var i in event["srcElement"]){
			pp+=(i+" "+event["srcElement"][i]+"\n");

		}
		alert(pp);
		*/

		//ÀÍ½ºÇÃ·Î¾î
		prams = "src=" + event["srcElement"]["parentElement"];
		prams += "&title=" + event["srcElement"]["title"];
	}

	prams +="&referer="+encodeURI(document.referrer);
	prams +="&count="+count;
	
	if(!writer) writer= new net.ContentLoader("http://www.siemensworld.com/new/test.php",write_ok,BuildError,"POST",prams);
}

var loggerPage;

function load()
{
	var prams;

	prams ="referer="+encodeURI(document.referrer);

	if(!loggerPage) {
		loggerPage= new net.ContentLoader("http://www.siemensworld.com/new/loggerPage.php",loading_ok,BuildError,"POST",prams);
	}
}
load();
