var Call = {
	init: function(params){
		this.params = params;
		var $Ca = this;

		// information in popup
		$('body .callInPopup')
			.click(function(){
				$Ca.callTextInPopup(this, 350);
				return false;
			});

		// congratulations popup
		$('body .callCongratulationInPopup')
			.click(function(){
				$Ca.callTextInPopup(this, 650);
				return false;
			});


        $('body .callSendFriend')
            .click(function(){
                $Ca.sendFriend();
                return false;
            });

        // send_sms recipients form
        $('body a#sendSmsRecipients')
            .click(function(){
                $Ca.sendSmsRecipients(this, 400);
            });

		// outlets in MAXIMA
		var sourceOutletsInMaxima = $('body div.callOutletsInMaxima');
		if(0 < sourceOutletsInMaxima.length)
			$Ca.callOutletsInMaxima(sourceOutletsInMaxima);

		// count downs
		var sourceCountDownds = $('body div.callCountDown')
		if(0 < sourceCountDownds.length)
			$Ca.callCountDown(sourceCountDownds);
	},

    sendFriend: function(){
        this._addonPopup(450, '<iframe width="410" height="280" frameborder="0" scrolling="no" src="' + this.params.webroot + '/c/send_friend"></iframe>', 'sendFriend');
    },

    sendSmsRecipients: function(obj, width)
    {
        var $Common = this;

        $.get(this.params.webroot + '/selfcare/sms/recipients/', function(response){
            $Common._addonPopup(540, response, 'text');
            var $form = $('form#selectRecipients');
            window.SendSmsRecipients.init({
                $all: $('select#all', $form),
                $selected: $('select#recipients', $form),
                $includer: $('input#includeSelected', $form),
                $excluder: $('input#excludeSelected', $form),
                $closer: $('input[type=reset]', $form),
                $form: $form
            });
        });
    },

	callTextInPopup: function(obj, width){
		var $Ca = this;
		var $obj = $(obj);
		var item = $obj.attr('id');
		var uri = $Ca.params.webroot +'/c/json/' + encodeURI(item);

		if(item)
			$.getJSON(uri, function(data){
				$Ca._addonPopup(width, data, 'text');
			});
	},

    _addonPopup: function(width, data, className){
        $.blockUI({
                css: {
                    left:'50%',
                    marginLeft: -(width/2) + 'px',
                    marginTop: '250px',
                    position:'absolute',
                    top:0,
                    padding:0,
                    width: width + 'px',
                    textAlign:'left',
                    color:'#000',
                    border:0,
                    backgroundColor:'#fff',
                    cursor:'default',
                    zIndex:9999999
                },
                overlayCSS: {
                    backgroundColor:'#000',
                    opacity:'0.3',
                    cursor: 'default',
                    zIndex:999999
                },
                message: '<div id="addonPopup" class=' + className + '>'
                + '<a href="javascript:;" class="close" onclick="$.unblockUI();">'
                //+ '<img src="img/common/close.greenish.gif" alt="" />'
                + '</a>'
                + '<p>'
                + data
                + '</p>'
                + '</div>'
            });
    },

	callOutletsInMaxima: function($source){
		var $Ca = this;
		var uri = $Ca.params.webroot +'/c/json/maxima_outlets';

		$source.html('<div class="cities"></div><ul class="output"></ul>');

		$.getJSON(uri, function(data){
			var output = {cities:[], list:{}};
			var $cities = $('div.cities', $source);
			var $output = $('ul.output', $source);

			if(data)
				var c = 0;
				$.each(data, function(city,list){
					$city	= $('<a href="javascript:;" title="'+ city +'">'+ city +'</a>')
								.click(function(){
									$('a', $cities).removeClass('active');
									$(this).addClass('active');

									$output.empty();
									$.each(list, function(i,item){
										$output.append('<li>'+ item[0] + ', <strong>'+ item[1] +'</strong></li>')
									});

									return false;
								});

					$cities.append(c++ > 0 ? ', ' : null);
					$cities.append($city);

					if(city in {'Vilnius':null})
						$city.click();
				});

				$cities.append('.');
		});
	},

	callCountDown: function($source){
		var $CD = this;
		var d = $source.html().replace("\n", "").split('|');
		if(7 == d.length){
			endDate = new Date(d[0],d[1],d[2],d[3],d[4],d[5],d[6]);
			endTime = endDate.getTime();
			var interval = setInterval(function(){
				$source.html($CD.getFull(endTime));
			}, 50);
		}
	},

	getDifference: function(endTime){
		tD = new Date();
		tT = tD.getTime();
		return (endTime - tT);
	},

	getDays: function(endTime){
		var t = this.getDifference(endTime);
		t = t / 1000;
		t = t / 60;
		t = t / 60;
		t = Math.floor(t / 24);
		return t;
	},

	getFull: function(endTime){
		msecs = this.getDifference(endTime);
		tsec = this.getDays(endTime) % 100;
		secs = Math.floor(msecs / 1000);
		mins = Math.floor(secs / 60);
		hours = Math.floor(mins / 60);
		days = Math.floor(hours / 24);
		msecs = String(msecs % 1000);
		secs = String(secs % 60);
		mins = String(mins % 60);
		hours = String(hours % 24);
		days = String(days);
		while (msecs.length < 3)
			msecs = "0" + msecs;

		if (secs.length < 2)
			secs = "0" + secs;

		if (mins.length < 2)
			mins = "0" + mins;

		if (hours.length < 2)
			hours = "0" + hours;

		return (days + ":" + hours + ":" + mins + ":" + secs + ":" + msecs);
	}
};
