// Define variables
var time_to_change = 4000;
var default_selected = 0;
var timer = 0;

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

// Ready
jQuery().ready(function() {

	
	// Magazines
    if ($('#magazines').length) {
        $('#magazines ul').jcarousel({
            scroll: 1,
			auto: 5,
			wrap: 'both',
			animation: 'slow'
        });
    };
	
	// Catalogue
	if ($('.sidebar .box .catalogue.slider').length) {
		$('.sidebar .box .catalogue.slider h4').click(function() {
			$('.sidebar .box .catalogue.slider a').hide();
			$('a', $(this).parent()).show();
		});
		
		// Show first element
		$('.sidebar .box .catalogue.slider li:first').each(function() {
			$('a', $(this)).show();		 
		});
	};
	
	// Accents
	$('#accents ul li a').each(function() {
		var news_num = $('#accents ul li a').index($(this));
	
		$(this).bind("mouseenter",function(){
			if ($('#accents div:eq('+news_num+')').css('display') != 'block')
			{
				$('#accents ul li a').animate( { paddingTop:"10px", paddingBottom:"0px" }, { duration: 200, queue: false });
				
				$(this).animate( { paddingTop:"0px", paddingBottom:"10px" }, { duration: 200, queue: false });
	
				$('#accents div').css('display', 'none');
				$('#accents div:eq('+news_num+')').fadeIn('fast');
			}
		}).bind("mouseleave",function(){
			
		}).bind("mouseover",function(){
			clearTimeout(timer);
		});
	});
	
	var accent_size = $('#accents ul li a').length;
	trigger_accent();
	
	function trigger_accent() {
		if(default_selected == accent_size) { default_selected=0; }	
		default_selected++;	
		$('#accents ul li a:eq('+(default_selected-1)+')').trigger('mouseenter');
		
		timer = setTimeout(function(){ trigger_accent(default_selected); }, time_to_change)
	}
	
    $('#header .navigation ul > li').bind('mouseover', jsddm_open);
    $('#header .navigation ul > li').bind('mouseout',  jsddm_timer);
	
	$('.blankable').focus(function() {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	
	$('.blankable').blur(function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});	
	
	$('.paginator #category_filter_top input[type=checkbox], .paginator #category_filter_bottom input[type=checkbox]').click(function() {
		url = location.href.toString();
		if (url.lastIndexOf('category') != -1) {
			url = url.substr(0, url.lastIndexOf('category') + 8) + '/';
		}

		ids = new Array();
		$('.paginator #' + $(this).parents('div')[0].id + ' input:gt(0):checked').each(function() {
			ids.push($(this).get(0).id.toString().replace(/([a-z_])+/, ''));
		});
		
		if ($(this).get(0).id == $('.paginator #' + $(this).parents('div')[0].id + ' input[type=checkbox]:first').get(0).id || ids.length == 0) {
			location.href = url + $(this).get(0).id.toString().replace(/([a-z_])+/, '');
		} else {
			location.href = url + ids.join(',');
		}
	});	
	
	$('.paginator #country_filter_top input[type=checkbox], .paginator #country_filter_bottom input[type=checkbox]').click(function() {
		url = location.href.toString();
		if (url.lastIndexOf('country') != -1) {
			url = url.substr(0, url.lastIndexOf('country') + 7) + '/';
		}
		ids = new Array();
		$('.paginator #' + $(this).parents('div')[0].id + ' input:gt(0):checked').each(function() {
			parts = $(this).get(0).id.toString().split('_');
			id = parts[parts.length - 1];
			ids.push(id);
		});
		
		if ($(this).get(0).id == $('.paginator #' + $(this).parents('div')[0].id + ' input[type=checkbox]:first').get(0).id || ids.length == 0) {
			location.href = url + 'all';
		} else {
			location.href = url + ids.join(',');
		}
	});	
	
	$('.paginator #downloads_filter_top input[type=checkbox], .paginator #downloads_filter_bottom input[type=checkbox]').click(function() {
		url = location.href.toString();
		if (url.lastIndexOf('downloads') != -1) {
			url = url.substr(0, url.lastIndexOf('downloads') + 9) + '/';
		}
		ids = new Array();
		$('.paginator #' + $(this).parents('div')[0].id + ' input:gt(0):checked').each(function() {
			parts = $(this).get(0).id.toString().split('_');
			id = parts[parts.length - 1];
			ids.push(id);
		});
		if ($(this).get(0).id == $('.paginator #' + $(this).parents('div')[0].id + ' input[type=checkbox]:first').get(0).id || ids.length == 0) {
			location.href = url + 'all';
		} else {
			location.href = url + ids.join(',');
		}
	});	
	
		
		function custom_click_change() {
		url = window.location.href.toString();
		if (url.lastIndexOf('/') != -1) {
			url = url.substr(0, url.lastIndexOf('/')+1);
		}
		

		ids = [];
		
		
		$('.categories_grabber input:checked , .categories_grabber select').each(function(){			
			if(this.value) {
				ids.push(this.value);
			}
		});
		
		
		
	
		
		window.location.href = url+ids.join(',');
	}
	
	$('.categories_grabber input[type=checkbox]').bind('click',function(){custom_click_change();});
	$('.categories_grabber input[type=radio]').bind('change',function(){custom_click_change();});
	$('.categories_grabber select').bind('change',function(){custom_click_change();});
	
	
	
	$('.paginator #flashgames_filter_top input[type=checkbox], .paginator #flashgames_filter_bottom input[type=checkbox]').click(function() {
		url = location.href.toString();
		if (url.lastIndexOf('flashgames') != -1) {
			url = url.substr(0, url.lastIndexOf('flashgames') + 10) + '/';
		}
		ids = new Array();
		$('.paginator #' + $(this).parents('div')[0].id + ' input:gt(0):checked').each(function() {
			parts = $(this).get(0).id.toString().split('_');
			id = parts[parts.length - 1];
			ids.push(id);
		});
		if ($(this).get(0).id == $('.paginator #' + $(this).parents('div')[0].id + ' input[type=checkbox]:first').get(0).id || ids.length == 0) {
			location.href = url + 'all';
		} else {
			location.href = url + ids.join(',');
		}
	});
	
	$('.article_images').click(function() {
		$('#article_image')[0].src = $('img', $(this))[0].src.replace('thumb_60x57_', 'thumb_350x250_');
	});	
	
	$('.gadget_images a').click(function() {
		$('#gadget_image')[0].src = $('img', $(this))[0].src.replace('thumb_80x80_', 'thumb_443x332_');
	});
	
	$('#do_compare').click(function() {
		window.location = $(this).parents('form')[0].action + $('#devices').val();
	});
	
	$('#search_button').click(function() {
		$(this).parents('form')[0].submit();
	});
	
	$('#search_mode').click(function(){
		if ($(this).attr('rel') * 1 == 1) {
			$('#advanced_search').show();
			$(this).attr('rel', 2);
		} else {
			$('#advanced_search').hide();
			$(this).attr('rel', 1);
		}
		val = $(this).text();
		$(this).text($(this).attr('title'));
		$(this).attr('title', val);
	});
	
	// Game
	$('#play-buttons li a').bind("mouseenter", function() {
		$($('#answers li')[$('#play-buttons li a').index(this)]).css("background-position", "-15px -267px");
	}).bind("mouseleave", function() {
		$($('#answers li')[$('#play-buttons li a').index(this)]).css("background-position", "-15px -15px");
	});
	
	$('#play-buttons a').click(function() {
		$('#answer_id').val($(this).attr("id").replace('answer_id_', ''));
		$.get(xhr+'?method=check_question',{question_id : $("#question_id").val(), answer_id : $('#answer_id').val()},function(data){
			$("#answers li:eq("+data+")").effectMe();
		});
	});
	
	$('#game-container #hint a').click(function() {
		$.get(xhr+'?method=hint_used',{question_id : $("#question_id").val()});
	});
	
	
});

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

var max_effect_me_blinks = 7;
var effect_me_blinks = 7;
var effect_me_timeout = 400;

jQuery.fn.effectMe = function () {
    return this.each(function () {
       var t = jQuery(this);
	   if(effect_me_blinks > 0) {
       	setTimeout(function(){
       		position = (effect_me_blinks%2 ==0) ? '-567px' : '-15px';
       		t.css("background-position", "-15px "+position);
       		--effect_me_blinks;
       		t.effectMe();
       	},effect_me_timeout);
	   }
	   else {
	   	effect_me_blinks = max_effect_me_blinks;
	   	setTimeout(function(){
			$('#question-answer').trigger("submit");
		},0);
	   }
    });
};




// Subnavigation
function jsddm_open()
{
   jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('.sub').css('display', 'block');
   var position = $(this).position();
   ddmenuitem.css('left', position.left);
   ddmenuitem.css('top', position.top + 33);
}

function jsddm_close()
{  
    if(ddmenuitem) ddmenuitem.css('display', 'none');
}

function jsddm_timer()
{  
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
    if(closetimer)
    {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

document.onclick = jsddm_close;
