/**
 * @author de-gha
 */

var cntParams = {};

// Just for Debugging
function outputObject(o) {
	var ret = '';
	for (var key in o)
	{
		ret = ret + key + ' : ' + o[key] + '\n';
	}
	return ret;
}

function initClickCount(selector)
{
	if ($(selector).size() > 0)
	{
		$(selector).each(function()
		{
			absUrl = false;
			campaign = 'shopClicks';
			css_id = $(this).parents('[@id]').attr('id').split('-');
			container = css_id[0];
			shop  = container.substring(7);
			ean = css_id[1];
			if (typeof ean != 'undefined')
			{
				ean = ean.substring(4);
			}
			href = $(this).attr('href');

			if (ergebnis = href.match(/^http:\/\//i))
			{
				absUrl = true;
			}
			else if (ergebnis = href.match(/^https:\/\//i))
			{
				absUrl = true;
			}

			if (absUrl)
			{
				newHref = 'http://cnt.pentax.de/RD/?campaign='+ campaign +'&container=' + container + '&ean=' + ean + '&shop=' + shop + '&callback=' + urlencode(href);
				$(this).attr('href', newHref);
			}
		});
	}
}

/**
* open new window with following parameters, all further settings active
*
* @param string link
* @param string wname
* @param int xsize
* @param int ysize
* @param int xpos
* @param int ypos
* @return bool (false)
*/

	function wopenfull(link,wname,xsize,ysize,xpos,ypos)
	{
		if (xsize==-1)
			xsize=screen.availWidth-10;
		if (ysize==-1)
			ysize=screen.availHeight-60;
		if (xpos==-1)
			xpos=(screen.availWidth-xsize)/2;
		if (ypos==-1)
			ypos=(screen.availHeight-ysize)/2;

   	features = 'width='+xsize+',height='+ysize+',status=no,location=yes,directories=no,menubar=yes,toolbar=yes,status=yes,scrollbars=yes,resizable=yes,dependent=no';

		nwindow  = open(link, wname, features);
		nwindow.focus();

		return false;
	}

// $(document).ready(function()

function initCoCo()
{
	$('div.shop').hover(
		function() {
			$('div.shop_info', this).fadeIn();
		},
		function() {
			$('div.shop_info', this).fadeOut();
		}
	);

	initClickCount('div.shop_info a[href]');
}

function urlencode( str ) {

    var histogram = {}, tmp_arr = [];
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });

    return ret;
}