/**
  Web click logging script
  2009. 02. 05. (Έρ) 10:48:44 KST
*/
Function.prototype.bind = function(){
  var _$A = function(a){return Array.prototype.slice.call(a);}
  if(arguments.length < 2 && (typeof arguments[0] == "undefined")) return this;

  var __method = this, args = _$A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat(_$A(arguments)));
  }
};

var Clicker = {
  RECORD_PATH: "http://www.sayclub.com/global/clicker.nwz",
  SHOW_PATH  : "http://www.sayclub.com/global/clicker.nwz?action=show",
  //SHOW_PATH  : "http://dev.ssc.neowiz.com/~nahoya/NL/www/global/clicker.nwz?action=show",
  WRAPPER_ID: '',
  IMG: new Image(),
  RECURSION: 0,

  click: function(wrapperId)
  {
    this.WRAPPER_ID = wrapperId ? wrapperId : '#sidebar';
    $("body").click(this.write_click.bind(this));
  },

  write_click: function(event)
  {

//alert(event.target.href);
//alert(event.target.onclick );
//alert(event.target.attributes['onclick'].value );
    // communication bar click log
    var commbarCoords;
    if ((commbarCoords = this.commbar_pos(event, event.target)))
    {
      var commPos = $("#commbarWrap").offset();
      if (typeof (commPos) != 'undefined')
        commbarCoords.y = commbarCoords.y - commPos.top;
      else
        commbarCoords.y = -1;
      if(commbarCoords.x > 0 && commbarCoords.y >0)
      {
        this.IMG.src = this.RECORD_PATH + 
          '?pv=' + this.mwtu_encode('http://commbar') +
          '&x='   + commbarCoords.x +
          '&y='   + commbarCoords.y ;
        this.IMG.onload= function(){}
        return;
      }
    }
    var curUrl = document.location.href;
    if (curUrl.indexOf("/ilike/music/track") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/ilike/music/track"))+"/ilike/music/track";
    else if (curUrl.indexOf("/ilike/music/artist") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/ilike/music/artist"))+"/ilike/music/artist";
    else if (curUrl.indexOf("/saycast/room/station") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/saycast/room/station"))+"/saycast/room/station/m4u";
    else if (curUrl.indexOf("/station/home/index") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/station/home/index"))+"/station/home/index/m4u";
    else if (curUrl.indexOf("/profile/home/view/") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/profile/home/view/"))+"/profile/home/view/skyblue_girl";
    var coords = this.get_position(event);
    var myurl = this.get_clickurl(event);
    this.IMG.src = this.RECORD_PATH + 
      '?pv=' + this.mwtu_encode(curUrl) +
      '&x='   + coords.x +
      '&y='   + coords.y + 
      '&linkurl=' + this.mwtu_encode(myurl) ;
    this.IMG.onload= function(){}
    return;
  },

  get_position: function(e)
  {
    if (e.button == 2) return;

    var coords = {'x':0, 'y':0};
    var srElement = e.target;
    var wrapperPos = $(this.WRAPPER_ID).offset();
    var contWidth = $(document).width();
    coords.x = e.pageX - wrapperPos.left;
    coords.x = e.pageX - wrapperPos.left;
    coords.y = e.pageY;

    return coords;
  },
  get_clickurl: function(e)
  {
    if (e.button == 2) return;
	
	var myHref = "";
	var myOnclick= "";

    var srElement = e.target;

	if (srElement.tagName == "A")
	{
		myHref = srElement.attributes['href'].value;

    if (srElement.attributes['onclick']) myOnclick= srElement.attributes['onclick'].value;

		if(myHref.length > myOnclick.length)  return myHref;
		else return myOnclick;
	}
	else if (srElement.tagName == "IMG")
	{
    if (srElement.attributes['onclick']) myOnclick= srElement.attributes['onclick'].value;
        
		if(myOnclick == 'null' || myOnclick == "") 
		{
			var p = $(srElement).parent()[0];
			if(p && p.tagName == "A" ) {

				myHref = p.attributes['href'].value;
        if (p.attributes['onclick']) myOnclick= p.attributes['onclick'].value;
          
				if(myHref.length > myOnclick.length)  return myHref;
				else return myOnclick;
			}
			else  return "";
		}
		else 
		{
			return myOnclick;	
		}
	}
	else return "";
  },
  commbar_pos: function(e, srElement)
  {
    var coords = {'x':0, 'y':0};
    try {
      if (srElement.tagName == 'DIV' && srElement.id == 'commbar')
      {
        coords.x = e.pageX;
        coords.y = e.pageY;
        return coords;
      }
      else
      {
        if (this.RECURSION == 10) 
          return coords;
        else
          return this.commbar_pos(e, $(srElement).parent()[0]);
      }
    }catch(e)
    {
      return coords;
    }
    this.RECURSION ++;
  },

  mwtu_encode : function (str, flag)
  {
    if(typeof(encodeURIComponent) == "function")
      return (flag) ? encodeURI(str) : encodeURIComponent(str);
    
    return escape(str);
  },

  show: function(date, wrapperId)
  {
    this.WRAPPER_ID = wrapperId ? wrapperId : '#sidebar';

    var curUrl = document.location.href;
    if (curUrl.indexOf("/ilike/music/track") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/ilike/music/track"))+"/ilike/music/track";
    else if (curUrl.indexOf("/ilike/music/artist") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/ilike/music/artist"))+"/ilike/music/artist";
    else if (curUrl.indexOf("/saycast/room/station") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/saycast/room/station"))+"/saycast/room/station/m4u";
    else if (curUrl.indexOf("/station/home/index") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/station/home/index"))+"/station/home/index/m4u";
    else if (curUrl.indexOf("/profile/home/view/") > 0)
      curUrl = curUrl.substring(0, curUrl.indexOf("/profile/home/view/"))+"/profile/home/view/skyblue_girl";

    var request = this.SHOW_PATH + 
                  '&clickdate=' + date +
                  '&uri=' + this.mwtu_encode(curUrl);
    var wrapperPos = $(this.WRAPPER_ID).offset();
//alert(request);

    //$("body").prepend('<div id="clickLogImg" style="position:absolute;left:'+wrapperPos.left+';top:0;display:;"><img border="0" src="'+request+'"</div>');
    $("body").prepend('<div id="clickLogImg" style="position:absolute;z-index:1000;left:'
                        +wrapperPos.left+';top:0;display:;"><img src="'
                        +request+'" style="z-index:1000;"></div>');

  }
};
/*
try {
if ('undefined' != typeof(window.jQuery))
  $(document).ready(function() { Clicker.click(); });
else
  Clicker.click();
} catch(e) {}
*/
/*
  $(document).ready(function() { Clicker.show(<?=$date?>); });
*/

