$(document).ready(function(){
	
	var pathname = window.location + "";
	var lang = pathname.substr(7, 2);
	
	if(lang != 'en' && lang != 'pl' && lang != 'ru') lang = 'en';
	
	/*	
	$("input[type=file]").filestyle({ 
	     image: "/public/gfx/browse_" + lang + ".png",
	     imageheight : 22,
	     imagewidth : 110,
	     width : 190
	 });
	 */
	
	
	
	
	////////////////////////////////////////////
	// INFO BOX
	////////////////////////////////////////////
	
	$('.info_rel').mouseover(function(){
		var info = $(this).attr('rel');
		var windowWidth = $(window).width();
		
		$('.info_box').html(info).show();
		//$('#js_debug').show();
		
		$(this).mousemove(function(e){
			var testWidth = e.pageX + 20 + 320;
			
			if(windowWidth > testWidth)
			{
				var x = e.pageX + 20;
				var y = e.pageY + 20;	
			}
			else
			{
				var x = e.pageX - 330;
				var y = e.pageY + 20;
			}
			//$('#js_debug').html(testWidth);
			
			
			$('.info_box').css({
				top: y + 'px',
				left: x + 'px'
			});
		})
	});
	
	$('.info_rel').mouseout(function(){
		$('.info_box').hide();
	});
	
	////////////////////////////////////////////
	// INFO BOX AUTO
	////////////////////////////////////////////
	
	$('.info_rel_auto').mouseover(function(){
		var info = $(this).attr('rel');
		var windowWidth = $(window).width();
		
		$('.info_box_auto').html(info).show();
		//$('#js_debug').show();
		
		$(this).mousemove(function(e){
			var testWidth = e.pageX + 20 + 320;
			
			if(windowWidth > testWidth)
			{
				var x = e.pageX + 20;
				var y = e.pageY + 20;	
			}
			else
			{
				var x = e.pageX - 330;
				var y = e.pageY + 20;
			}
			//$('#js_debug').html(testWidth);
			
			
			$('.info_box_auto').css({
				top: y + 'px',
				left: x + 'px'
			});
		})
	});
	
	$('.info_rel_auto').mouseout(function(){
		$('.info_box_auto').hide();
	});
	
	////////////////////////////////////////////
	// SUPPORT FORM
	////////////////////////////////////////////
	
	$('#support_form_required').submit(function(){
		var info = "";
		
		$('select.required_form').each(function(){
			if($(this).val() == "")                        
			{
				$(this).css('border-color', '#f00');
				info += $(this).next().val() + "\n";
			}
			else $(this).css('border-color', '#464646');
		});
		
		$('input.required_form').each(function(){
			if($(this).val() == "") 
			{
				$(this).css('border-color', '#f00');
				info += $(this).next().val() + "\n";
			}
			else 
			{
				if($(this).hasClass('check_email'))
				{
					var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
					
					if(!emailReg.test($(this).val()))
					{
						$(this).css('border-color', '#f00');
						info += $(this).next().next().val() + "\n";	
					}
					else
					{
						$(this).css('border-color', '#464646');
					}
				}
				else
				{
					$(this).css('border-color', '#464646');	
				}
			}
		});
		
		//if($('#support_dxdiag').val() == "") alert(123);
		if($('#support_dxdiag').val() == "")
		{
			$('.support_dxdiag input').each(function(){
				if($(this).val() == "") 
				{
					$(this).css('border-color', '#f00');
					info += $(this).next().val() + "\n";
				}
				else $(this).css('border-color', '#464646');
			});
		}
		
		if(info != "")
		{
			alert(info);
			return false;
		}

	});
	
	////////////////////////////////////////////
	// LANG BOX PRELOAD
	////////////////////////////////////////////
	
	$('.lang_list').children().each(function(){
		if($(this).attr('selected') == 'selected')
		{
			$('.lang_box').html($(this).html());
		}
	});
	
	$('.lang_list').hide(1, function(){
		$('.lang_box').show(1);
		$('.lang_box_end').show(1);
	});
	
	$('.lang_title, .lang_box_end, .lang_box, .lang_box_selected').click(function(){
		if($('.lang_box_options').css('display') == 'none')
		{
			$('.lang_box_options').slideDown(100);
		}
		else
		{
			$('.lang_box_options').slideUp(100);	
		}
	});

	$(document).bind('click', function(e) {
		var className = e.target.className;
		var classes = className.split(" ");
		var stop = 0;
			 
		for (var i=0; i<classes.length; i++)
		{
			if(classes[i].indexOf('lang_title') == 0) stop = 1;
			if(classes[i].indexOf('lang_box_end') == 0) stop = 1;
			if(classes[i].indexOf('lang_box') == 0) stop = 1;
			if(classes[i].indexOf('lang_box_selected') == 0) stop = 1;
		}
		
		if(stop != 1) $('.lang_box_options').slideUp(100);

     });
     
     $('.age_input').mouseover(function(){
		//$(this).css('border-color', '#f90');
		
	});
     
     ////////////////////////////////////////////
	// CHANGE GALLERY
	////////////////////////////////////////////
	
	$('.change_gallery').click(function(){
		var url = $(this).attr('href');
		var title = $(this).attr('title');
		
		$('.w2_media_div img').attr('src', url);
		
		$('.slider_items a').each(function(){
			$(this).children().removeClass('gallery_item_selected');
		});
		
		$(this).children().addClass('gallery_item_selected');
		
		if(title)
		{
			$('.wall_selected').removeClass('wall_selected');
			$('#' + title).addClass('wall_selected');

		}
		
		return false;
	});
	
	////////////////////////////////////////////
	// CHANGE VIDEO
	////////////////////////////////////////////
	
	$('.change_video').click(function(){
		var url = $(this).attr('href');
		var title = $(this).attr('title');
		
		$('.w2_media_div').flash({
			swf: url,
			allowFullScreen: true,
			width: 980,
			height: 576
		});
		
		$('.slider_items a').each(function(){
			$(this).children().removeClass('video_item_selected');
		});
		
		$(this).children().addClass('video_item_selected');

		return false;
	});
	
	if($('#video_autoload').val())
	{
		$('.w2_media_div').flash({
			swf: $('#video_autoload').val(),
			allowFullScreen: true,
			width: 980,
			height: 576
		});
	}
	
	////////////////////////////////////////////
	// SLIDER
	////////////////////////////////////////////
	
	
	
	$('.slider_arrow_right').click(function(){
     	var pages = $('#slider_pages').val();
     	var current_page = $('#slider_current_page').val();
     	
     	if(current_page != pages)
     	{
			current_page++;
			$('#slider_current_page').val(current_page);
			
			$('.slider_center').animate({
				scrollLeft: '+=905'
			});
		}
		
		if(current_page > 0) $('.slider_arrow_left').addClass('slider_arrow_left_on');
		if(current_page == pages) $('.slider_arrow_right').removeClass('slider_arrow_right_on');
		
	});
	
	$('.slider_arrow_left').click(function(){
     	var pages = $('#slider_pages').val();
     	var current_page = $('#slider_current_page').val();
     	
     	if(current_page != 0)
     	{
			current_page--;
			$('#slider_current_page').val(current_page);
			
			$('.slider_center').animate({
				scrollLeft: '-=905'
			});
		}
		
		if(current_page == 0) $('.slider_arrow_left').removeClass('slider_arrow_left_on');
		if(current_page < pages) $('.slider_arrow_right').addClass('slider_arrow_right_on');
		
	});
	
	$('#support_form').click(function(){
		if($('.support_form').css('display') == 'none') $('.support_form').slideDown();
		else $('.support_form').slideUp();
		
		return false;
	});
	
	$('#support_dxdiag').change(function(){
		$('.support_dxdiag').slideUp();
	});
	
	
	////////////////////////////////////////////
	// TOP ALERT
	////////////////////////////////////////////

	$('.top_alert_anim').animate({
		top: 0
	}, 1000);
	
	
	////////////////////////////////////////////
	// NEWS
	////////////////////////////////////////////
	
	$('.community_news2_more').click(function(){
		var limit = $(this).attr('rel').split(',');
		var good = 0;
		
		$.ajax({
			url: "http://" + limit[2] + ".thewitcher.com/index.php?c=community_overview2&m=ajax_news&p=" + limit[0] + '|' + limit[1],
			cache: false,
			success: function(html){ 
				$('.community_news2_dynamic').append(html);
				good = 1;
			}
		});
		
		if(good == 0)
		{
			$.ajax({
				url: "http://thewitcher.com/index.php?c=community_overview2&m=ajax_news&p=" + limit[0] + '|' + limit[1],
				cache: false,
				success: function(html){ 
					$('.community_news2_dynamic').append(html);
				}
			});	
		}
		
		limit[0] = parseInt(limit[0]);
		limit[0] += parseInt(limit[1]);

		$.ajax({
			url: "http://" + limit[2] + ".thewitcher.com/index.php?c=community_overview2&m=ajax_check_news&p=" + limit[0],
			cache: false,
			success: function(html){
				
				if(html == 1) $('.community_news2_more').attr('rel', (limit[0] + ',' + limit[1] + ',' + limit[2]));
				else $('.community_news2_more').hide();
			}
		});
		
		
          if(good == 0)
		{
			$.ajax({
				url: "http://thewitcher.com/index.php?c=community_overview2&m=ajax_check_news&p=" + limit[0],
				cache: false,
				success: function(html){
					
					if(html == 1) $('.community_news2_more').attr('rel', (limit[0] + ',' + limit[1] + ',' + limit[2]));
					else $('.community_news2_more').hide();
				}
			});	
		}
		
		
		return false;
	});
	
	////////////////////////////////////////////
	// ROTATOR
	////////////////////////////////////////////
	
	var rotator_click = false;
	var rotator_mousedown = true;
	var rotator_click_x = 0;
	var rotator_margin = 0;
	var rotator_autoscroll = true;                 
	var rotator_autoscroll_time = 5000;
	var rotator_timer = setTimeout(rotator_auto, rotator_autoscroll_time);
	
	$('.rotator_img_box').mouseover(function(){
		$('.rotator_arrows_left').show();
		$('.rotator_arrows_right').show();
	});
	
	$('.rotator_img_box').mouseout(function(){
		$('.rotator_arrows_left').hide();
		$('.rotator_arrows_right').hide();
	});
	
	$('.rotator_arrows_left').mouseover(function(){
		$('.rotator_arrows_left').show();
		$('.rotator_arrows_right').show();
		$(this).addClass('rotator_arrows_left_on');
	});
	
	$('.rotator_arrows_left').mousedown(function(){
		if(rotator_mousedown == false) return; 
		rotator_autoscroll = false;
		
		rotator_current = $('#current').val();

		if(rotator_current == 0) rotator_current = 4;
		else rotator_current--;
		
		$('#current').val(rotator_current);
		
		var link = $('#rotator_link_' + rotator_current).val();
		var img = '/public/files/rotator/' + $('#rotator_img_' + rotator_current).val();
		var title = $('#rotator_title_' + rotator_current).val();
		var txt = $('#rotator_txt_' + rotator_current).val();

		rotator_current *= 112;
		
		$('#rotator_margin').val(rotator_current);

		$('#target_link').attr('href', link);
		$('#target_img').attr('src', img);
		$('#target_title').html(title);
		$('#target_txt').html(txt);

		rotator_autoscroll = true;
		rotator_timer = clearTimeout(rotator_timer);
		rotator_timer = setTimeout(rotator_auto, rotator_autoscroll_time);
	});
	
	$('.rotator_arrows_left').mouseout(function(){
		$(this).removeClass('rotator_arrows_left_on');
		$('.rotator_arrows_left').hide();
		$('.rotator_arrows_right').hide();
	});
	
	$('.rotator_arrows_right').mouseover(function(){
		$('.rotator_arrows_left').show();
		$('.rotator_arrows_right').show();
		$(this).addClass('rotator_arrows_right_on');
	});
	
	$('.rotator_arrows_right').mousedown(function(){
		if(rotator_mousedown == false) return; 
		rotator_autoscroll = false;
		
		rotator_current = $('#current').val();

		if(rotator_current == 4) rotator_current = 0;
		else rotator_current++;
		
		$('#current').val(rotator_current);
		
		var link = $('#rotator_link_' + rotator_current).val();
		var img = '/public/files/rotator/' + $('#rotator_img_' + rotator_current).val();
		var title = $('#rotator_title_' + rotator_current).val();
		var txt = $('#rotator_txt_' + rotator_current).val();

		rotator_current *= 112;
		
		$('#rotator_margin').val(rotator_current);

		$('#target_link').attr('href', link);
		$('#target_img').attr('src', img);
		$('#target_title').html(title);
		$('#target_txt').html(txt);

		rotator_autoscroll = true;
		rotator_timer = clearTimeout(rotator_timer);
		rotator_timer = setTimeout(rotator_auto, rotator_autoscroll_time);
	});
	
	$('.rotator_arrows_right').mouseout(function(){
		$(this).removeClass('rotator_arrows_right_on');
		$('.rotator_arrows_left').hide();
		$('.rotator_arrows_right').hide();
	});
	
	$('.rotator_suwak_buttons').mousedown(function(e){
		rotator_autoscroll = false;
		
		rotator_click = true;
		rotator_click_x = e.pageX;
		rotator_margin = parseInt($('#rotator_margin').val());
		$(this).addClass('rotator_suwak_buttons_down'); 
	});
	
	$(document).mouseup(function(){
		rotator_click = false;
		rotator_mousedown = false;
		
		var current = $('#current').val() * 112;
		
		$('.rotator_suwak_buttons').animate({
			marginLeft: current + 'px'
		}, 300, function(){
			rotator_mousedown = true;
		});
		
		$('#rotator_margin').val(current);
		
		$('.rotator_suwak_buttons').removeClass('rotator_suwak_buttons_down'); 
		
		rotator_autoscroll = true;
		rotator_timer = clearTimeout(rotator_timer);
		rotator_timer = setTimeout(rotator_auto, rotator_autoscroll_time);
	});
	
	$(document).mousemove(function(e){
		if(rotator_click == false) return;
		rotator_mousedown = false;      
		
		var rotator_move_x = rotator_margin + (e.pageX - rotator_click_x);
		
		if(rotator_move_x > 0 && rotator_move_x < 448)
		{
			$('#rotator_margin').val(rotator_move_x);
			$('.rotator_suwak_buttons').css('margin-left', rotator_move_x + 'px');
			
			var current = Math.round(rotator_move_x / 112);
			$('#current').val(current);
			
			var link = $('#rotator_link_' + current).val();
			var img = '/public/files/rotator/' + $('#rotator_img_' + current).val();
			var title = $('#rotator_title_' + current).val();
			var txt = $('#rotator_txt_' + current).val();
			
			$('#target_link').attr('href', link);
			$('#target_img').attr('src', img);
			$('#target_title').html(title);
			$('#target_txt').html(txt);
		}
		else if(rotator_move_x < 0)
		{
			$('#rotator_margin').val(0);
			$('.rotator_suwak_buttons').css('margin-left', '0px');
		}
		else if(rotator_move_x > 448)
		{
			$('#rotator_margin').val(448);
			$('.rotator_suwak_buttons').css('margin-left', '448px');
		}
		
	});
	
	function rotator_auto()
	{
		if(rotator_autoscroll == false) return;
		
		var current = $('#current').val();
		
		if(current == 4) current = 0;
		else current++;
		
		$('#current').val(current);
		
		var link = $('#rotator_link_' + current).val();
		var img = '/public/files/rotator/' + $('#rotator_img_' + current).val();
		var title = $('#rotator_title_' + current).val();
		var txt = $('#rotator_txt_' + current).val();

		current *= 112;
		
		$('#rotator_margin').val(current);
		
		if(current > 0)
		{
			$('.rotator_suwak_buttons').animate({
				marginLeft: current + 'px'
			}, 300, 'linear', function(){
				
				$('#target_link').attr('href', link);
				$('#target_img').attr('src', img);
				$('#target_title').html(title);
				$('#target_txt').html(txt);
				
				rotator_timer = setTimeout(rotator_auto, rotator_autoscroll_time);
			});
		}
		else
		{
			$('.rotator_suwak_buttons').css('margin-left', '0px');
			
			$('#target_link').attr('href', link);
			$('#target_img').attr('src', img);
			$('#target_title').html(title);
			$('#target_txt').html(txt);
				
			rotator_timer = setTimeout(rotator_auto, rotator_autoscroll_time);
		}
	}


     ////////////////////////////////////////////
	// COUNTDOWN OPEN
	////////////////////////////////////////////
	$(function () {
		var austDay = new Date(2012, 0,26,19,0,0);
		$('#countdown').countdown({until: austDay, format: 'HMS', compact: true,
		expiryText: ' '});
	});
	
	////////////////////////////////////////////
	// CHECK AGE
	////////////////////////////////////////////
	
	$('.js_day').focus(function(){
		$(this).parent().addClass('age_input_selected');
	});
	
	$('.js_day').blur(function(){
		if($(this).val() == '') $(this).val('1');
		$(this).parent().removeClass('age_input_selected');
	});
	
	$('.js_month').focus(function(){
		$(this).parent().addClass('age_input_selected');
	});
	
	$('.js_month').blur(function(){
		if($(this).val() == '') $(this).val('1');
		$(this).parent().removeClass('age_input_selected');
	});
	
	$('.js_year').focus(function(){
		if($(this).val() == 'YYYY') $(this).val('');
		$(this).parent().addClass('age_input_selected');
	});
	
	$('.js_year').blur(function(){
		if($(this).val() == '') $(this).val('YYYY');
		$(this).parent().removeClass('age_input_selected');
	});
	
	$('#age_form').submit(function(){
		var day = parseInt($('.js_day').val());
		var month = parseInt($('.js_month').val());
		var year = parseInt($('.js_year').val());
		var stop = 0;
		//alert(day);
		if(!day || day == '' || day > 31) 
		{
			$('.js_day').parent().addClass('age_input_error');
			stop = 1;
		}
		else $('.js_day').parent().removeClass('age_input_error');
		
		if(!month || month == '' || month > 12)
		{    
			$('.js_month').parent().addClass('age_input_error');
			stop = 1;
		}
		else $('.js_month').parent().removeClass('age_input_error');
		
		if(!year || year == '') 
		{
			$('.js_year').parent().addClass('age_input_error');
			stop = 1;
		}
		else $('.js_year').parent().removeClass('age_input_error');
			
		if(stop == 1) return false;	
	});
	
	$('.anim_box_off a').click(function(){return false;})
	
	////////////////////////////////////////////
	// CHARACTERS
	////////////////////////////////////////////
	
	$('.switch_char').click(function(){
		var name = $(this).attr('rel');
		var id = $(this).attr('id');
		var concept = $('.' + name + '_concept');
		var render = $('.' + name + '_render');
		
		switch(id)
		{
			case 'concept':
				if(concept.css('display') == 'none')
				{
					render.fadeOut(300);
					concept.fadeIn(300);
					$(this).addClass('selected');
					$('#render').removeClass('selected');		
				}
			break;
			
			case 'render':
				if(render.css('display') == 'none')
				{
					concept.fadeOut(300);
					render.fadeIn(300);
					$(this).addClass('selected');
					$('#concept').removeClass('selected');		
				}
			break;
		}
		
		return false;
	});

});
