$(document).ready(function(){
	var cb = function(i){
		advClickArea(0,i,true,cb,400); 
	}
	cb(0);
});

function advClickArea(id,i,timer,cb,maxlength){
	maxlength = maxlength || 185; // set defaults
	var expr = '#gameguru'+id+' .description_gameguru';
	if(jQuery(expr).get(0)){
		if(!jQuery(expr+' a').get(0)){
			jQuery(expr).each(function(){
				var link = $(this).parent().find('a').attr('href');
				var words = $(this).html().split(' ');
				var text = '';
				for(var i in words){
					if( (text.length + words[i].length + 1) < maxlength){
						text += ' '+words[i]
					}else{
						text += ' ...';
						break;
					}
				}
				$(this).html('<a href="'+link+'" title="'+$(this).html()+'" target="_blank">'+text+'</a>');
			});
		}
	}else if(timer){
		if(!i){ i=0 }
		if(i>10) return;
		setTimeout(function(){
			i++;
			cb(i)
		},500)
	}
}

function Adv(id,limit){
    this.block = '#gameguru'+id+' ';
    this.limit = limit;
    this.timer = null;
    this.id = id
    this.time = 5; //сколько времени проверять загрузку банера
    this.searchcount = 0; // сколько раз пыталось найти
    var self;
    this.init = function(){
	//console.log(this.block);
	self = this;
	this.timer = setInterval(search,1000);
    };
    this.click = function(){
	var id = self.getLinkId(this);
	$.cookie('gg'+id, '1', { expires: 1 });
	return true;
    };
    
    this.getLinkId = function(link){
	var reg= /id=([0-9]+)/  
	var res = reg.exec(link);
	return res[1];
    }
    
    this.rotate = function(){
	
	jQuery(this.block+'  a').live('click',self.click);
	    var adv_num = 0;
	    jQuery(self.block+' .header_gameguru a').each(function(){
		var link =  $(this).attr('href');
		var id = self.getLinkId(link);
		var hide = false;
		if($.cookie('gg'+id)){
		    hide = true;
		}else{
		    adv_num ++;
		}
		if(adv_num>self.limit || hide){
		    //console.log('hide in block:'+self.id+' adv id:'+id)
		    $(this).parents('.gameblock_gameguru').hide();
		}
	    });
	
	//;
    }
    
    function search(){
	self.searchcount ++;
	if(self.searchcount > self.time) {
	    clearInterval(self.timer);
	    //console.log('stop');
	    return;
	}
	//console.log(self.block+'.header_gameguru a')
	if(jQuery(self.block+'.gameblock_gameguru a').get(0)){
	    clearInterval(self.timer);
	    //console.log('Found'+self.id);
	    self.rotate();
	}else{
	    //console.log('searching ...');
	    return false;
	}
    }
}

new Adv(0,20).init();
new Adv(1,1).init();
new Adv(2,2).init();
new Adv(3,2).init();
new Adv(10,1).init();
new Adv(5,2).init();







jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
