/**
 * CU popup
 */

var _domain = '.demoty.pl';
var _jquery_noconflict = false;

var _cu_width = 310;
var _cu_height = 104;

var _pic_width = 92;
var _pic_height = 72;

var _scroll_display = 150;

var _cu_do_not_hide = true;

var _cu_scroll_last;
var _cu_content;
var _cu_loading = false;

var x_image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAYAAADgkQYQAAAC7mlDQ1BJQ0MgUHJvZmlsZQAAeAGFVM9rE0EU/jZuqdAiCFprDrJ4kCJJWatoRdQ2/RFiawzbH7ZFkGQzSdZuNuvuJrWliOTi0SreRe2hB/+AHnrwZC9KhVpFKN6rKGKhFy3xzW5MtqXqwM5+8943731vdt8ADXLSNPWABOQNx1KiEWlsfEJq/IgAjqIJQTQlVdvsTiQGQYNz+Xvn2HoPgVtWw3v7d7J3rZrStpoHhP1A4Eea2Sqw7xdxClkSAog836Epx3QI3+PY8uyPOU55eMG1Dys9xFkifEA1Lc5/TbhTzSXTQINIOJT1cVI+nNeLlNcdB2luZsbIEL1PkKa7zO6rYqGcTvYOkL2d9H5Os94+wiHCCxmtP0a4jZ71jNU/4mHhpObEhj0cGDX0+GAVtxqp+DXCFF8QTSeiVHHZLg3xmK79VvJKgnCQOMpkYYBzWkhP10xu+LqHBX0m1xOv4ndWUeF5jxNn3tTd70XaAq8wDh0MGgyaDUhQEEUEYZiwUECGPBoxNLJyPyOrBhuTezJ1JGq7dGJEsUF7Ntw9t1Gk3Tz+KCJxlEO1CJL8Qf4qr8lP5Xn5y1yw2Fb3lK2bmrry4DvF5Zm5Gh7X08jjc01efJXUdpNXR5aseXq8muwaP+xXlzHmgjWPxHOw+/EtX5XMlymMFMXjVfPqS4R1WjE3359sfzs94i7PLrXWc62JizdWm5dn/WpI++6qvJPmVflPXvXx/GfNxGPiKTEmdornIYmXxS7xkthLqwviYG3HCJ2VhinSbZH6JNVgYJq89S9dP1t4vUZ/DPVRlBnM0lSJ93/CKmQ0nbkOb/qP28f8F+T3iuefKAIvbODImbptU3HvEKFlpW5zrgIXv9F98LZua6N+OPwEWDyrFq1SNZ8gvAEcdod6HugpmNOWls05Uocsn5O66cpiUsxQ20NSUtcl12VLFrOZVWLpdtiZ0x1uHKE5QvfEp0plk/qv8RGw/bBS+fmsUtl+ThrWgZf6b8C8/UXAeIuJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAtUlEQVQYGWWPwQmEMBBFP5vtwzrcaxARrEEsJJttIofcvFiD1uBFD3YgKC62IMxmIhF0/yEDfz5vfkBO63fl8adlWbwHDuR5TsaYS6hpGkrTlPq+J/CmqiqSUp5BDiRJQvqjad93esKpLEseqOsa0zRhGAbErxj6rSGEgCeFO0opTyyKwhOC//AI97Rti67rEEUR5nmGtTasDtK9w70jtm2jLMvOkuFECDr68btxHC8dQpB91g9THOX+kmYrVgAAAABJRU5ErkJggg==';

if (_jquery_noconflict) {
	jQuery.noConflict();
}

/**
 * Returns a random integer between min and max
 * Using Math.round() will give you a non-uniform distribution!
 * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/random
 */
function getRandomInt(min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function _cu_load() {
	try {
		var count = _cu_content.contents.ad_mini.items.length;
		var item = _cu_content.contents.ad_mini.items[ getRandomInt(0, count-1) ];
	} catch (e) {
		jQuery(window).unbind('scroll');
		return;
	}
	
	var cu = jQuery('#cu_popup');
	var html = jQuery('<a id="'+item.id+'" class="'+_cu_content.contents.ad_mini.itemsClass+' cu_elem" href="'+item.url+'" target="_blank"></a>');
	html.append('<img src="'+item.ico+'" width="'+_pic_width+'" height="'+_pic_height+'" />');
	html.append('<span class="title">'+item.title+'</span>');
	html.append('<span class="text">'+item.text+'</span>');
	
	cu.append('<span class="cu_close">X</span>');
	cu.append(html);
	
	jQuery('.cu_close').click(function() {
		jQuery('#cu_popup').stop(true);
		jQuery('#cu_popup').animate({ right: -_cu_width-(_cu_height/5) }, 'slow');
		jQuery(window).unbind('scroll');
	})
	
	jQuery('.cu_elem').click(function() {
		jQuery.ajax({
			url: 'http://spot.o2.pl/content/click',
			dataType: 'jsonp',
			data: { id: jQuery(this).attr('id').split('-')[1], site: jQuery(this).attr('id').split('-')[2], url: jQuery(this).attr('href') }
		});
	});
	
}

function _cu_get_content() {
	if (_cu_loading || _cu_content) {
		return;
	}
	
	_cu_loading = true;
	
	jQuery.ajax({
		url: 'http://spot.o2.pl/get/ad_mini',
		success: function(data) {
			_cu_content = data;
			_cu_load();
			_cu_scroll();
						
			var cu_displayed = parseInt(jQuery.cookie('cu_displayed'));
			
			if (isNaN(cu_displayed)) {
				cu_displayed = 1;
			} else {
				cu_displayed++;
			}
			
			jQuery.cookie('cu_displayed', cu_displayed, { expires: 1, path: '/', domain: _domain });
		},
		dataType: 'jsonp',

		complete: function() {
			_cu_loading = false;
		}
	});
}

function _cu_scroll() {
	_cu_get_content();
	
	if (jQuery(window).scrollTop() >= _scroll_display && _cu_content) {
		if (_cu_scroll_last > jQuery(window).scrollTop())
			jQuery('#cu_popup').stop(true);
		jQuery('#cu_popup').animate({right: 0}, 'slow');
		if (_cu_do_not_hide) {
			jQuery(window).unbind('scroll');
		}
	} else {
		if (_cu_scroll_last < jQuery(window).scrollTop())
			jQuery('#cu_popup').stop(true);
		jQuery('#cu_popup').animate({ right: -_cu_width-(_cu_height/5) }, 'slow');
	}
	
	_cu_scroll_last = jQuery(window).scrollTop();
}

jQuery(document).ready(function () {
	
	if (jQuery.cookie('cu_displayed') >= 2) {
		return;
	}
	
	jQuery(window).scroll(_cu_scroll);
	
	var cu = jQuery('<div id="cu_popup" />');
	jQuery('body').append(cu);
	
	jQuery('body').append('<style> \
			.ie6 #cu_popup, .ie6 #cu_popup img, .ie7 #cu_popup, .ie7 #cu_popup img, .ie8 #cu_popup, .ie8 #cu_popup img { border: 1px solid #aaa !important; } \
			#cu_popup { z-index: 100; width: '+_cu_width+'px; height: '+_cu_height+'px; position: fixed; bottom: 0; right: -'+(_cu_width+(_cu_height/5))+'px; -moz-box-shadow: 0 0 18px #000; -webkit-box-shadow: 0 0 18px #000; box-shadow: 0 0 18px #000; background: #fff; } \
			#cu_popup .cu_close { display: block; width: 9px; height: 9px; background: url('+x_image+'); position: absolute; right: 0; top: 0; margin: 4px; cursor: pointer; text-indent: -9999em; } \
			.ie6 #cu_popup .cu_close, .ie7 #cu_popup .cu_close { text-indent: 0; font-family: Arial,Sans-serif; } \
			#cu_popup .cu_elem { display: block; position: absolute; top: 10px; left: 8px; text-decoration: none; width: 90%; border: none; } \
			#cu_popup img { float: left; padding: 4px; border: none; -moz-box-shadow: 0 0 8px #000; -webkit-box-shadow: 0 0 8px #000; box-shadow: 0 0 8px #000; margin: 4px; margin-right: 10px; } \
			#cu_popup .title { display: block; color: #00f; font-size: 14px; font-weight: bold; color: #0000FF; font-weight: bold; font-family: Arial,Sans-serif; margin: 6px 0; } \
			#cu_popup .text { display: block; color: #000; font-size: 12px; font-family: Arial,Sans-serif; } \
			</style>');
	
});

