var FSDL = {
   'baseUrl'   : 'freedownloadsoft.net',
   'defaultRef': '152159'
};

var ZCmapping = new Array();
ZCmapping[53995] = 147470;
ZCmapping[54057] = 147532;
ZCmapping[54060] = 147535;
ZCmapping[56504] = 155818;
ZCmapping[58922] = 159191;
ZCmapping[58923] = 159192;
ZCmapping[59278] = 160797;
ZCmapping[59279] = 160799;
ZCmapping[59910] = 161484;
ZCmapping[59912] = 161486;
ZCmapping[62926] = 166355;
ZCmapping[62927] = 166356;
ZCmapping[62928] = 166357;


$(document).ready(function(){
   PPNcookie();
});


/*
 * Move and modify content of the infoBox
 */
function moveDescBox(dom){
	if ($(dom).exists()){
		moveRateSystem('#myRate');
		$('.infosBox').insertAfter(dom);
	}
}

function moveRateSystem(dom){
	var txt = $('.ratingtext div').html().split(' (');
	$('.ratingtext div').html(txt[0] + '<br/>(' + txt[1]);
	$('.ratingblock').insertAfter(dom).show();
}


/*
 * Move and modify content of the infoBox
 */

function d2cLog(){
	setTimeout(function(){
		var img = new Image();
		img.src = 'http://install.'+ FSDL.baseUrl + '/log/software/impression/' + getChannelId() +'/'+ contentId +'/';
	},300);
}


/*
 * Build dlButton;
 */
function dlButton(){
   $('.dlButton').attr('href','http://install.'+ FSDL.baseUrl + '/installer/d2cdownload/' + getChannelId() +'/'+ contentId +'/');
};


/*
 * Get ChannelId
 */
function getChannelId(){
   var cookie = readCookie('ref');
   return (cookie != '')? cookie : readCookie('defaultRef');
};


/*
 * Set channelId by cookie
 */
function PPNcookie(){
   // Set a default refId
   writeCookie('defaultRef',FSDL.defaultRef,3600);

   // Ckheck if a ref exist
   var ref = QueryString.get('ref');
   if (ref){
      ref = (typeof ZCmapping[ref] != 'undefined')? ZCmapping[ref] : ref;
      writeCookie('ref',ref,3600);
   }
};


/*
 * Read cookie and return value if exist
 */
function readCookie(name){
var regex = new RegExp(name + "s*=s*(.*?)(;|$)");
var cookies = document.cookie.toString();
var match = cookies.match(regex);

if(match) {
// Got Cookie!
return unescape(match[1]);
}
return '';
}


/*
 * Write cookie
 */
function writeCookie(name, value, expireSeconds) {
   if (!expireSeconds) {
      expireSeconds = 365 * 24 * 60 * 60;
   }

   //Cookie need milliseconds
   expireSeconds = expireSeconds * 1000;

   var date = new Date();
   date.setTime(date.getTime() + expireSeconds);
   document.cookie = name + "=" + escape(value) + "; expires=" + date.toGMTString() + "; path=/";
   return true;
}



/*
 * Add exist function to jQuery
 */
jQuery.fn.exists = function(){return jQuery(this).length>0;}


//Wrapped the original object to be able to do Querystring.get instead
//of instantiating a new object each time.
var QueryString = (function() {

   /* Client-side access to querystring name=value pairs
      Version 1.3
      28 May 2008

      License (Simplified BSD):
      http://adamv.com/dev/javascript/qslicense.txt
   */
   function QueryString(qs) { // optionally pass a querystring to parse
      this.params = {};

      if (qs == null) qs = location.search.substring(1, location.search.length);
      if (qs.length == 0) return;

      // Turn <plus> back to <space>
      // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
      qs = qs.replace(/\+/g, ' ');
      var args = qs.split('&'); // parse out name/value pairs separated via &

      // split out each name=value pair
      for (var i = 0; i < args.length; i++) {
         var pair = args[i].split('=');
         var name = decodeURIComponent(pair[0]);

         var value = (pair.length==2)
            ? decodeURIComponent(pair[1])
            : name;

         this.params[name] = value;
      }
   }

   QueryString.prototype.get = function(key, default_) {
      var value = this.params[key];
      return (value != null) ? value : default_;
   }

   return {
      get : function(key, default_value, qs) {
         return (new QueryString(qs)).get(key, default_value);
      }
   }
})();
