function gol(field){
	if(field.val() == null || field.val() == ""){
		return true;
	} else {
		return false;
	}
}

function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
};

function isNumber(string){
	if(string.match(/^[0-9]+$/)){
		return true;
	} else {
		return false;
	}
}

function isText(string){
	if(string.match(/^[a-zA-Z]+$/)){
		return true;
	} else {
		return false;
	}
}

function ticket_update_summary()
{
    //$.fancybox.showActivity();
    $.ajax(
    {
        type: "POST",
        cache: false,
        url: "soap/ticket/",
        data: '&op=summary',
        success: function(html)
        {
            //$.fancybox.hideActivity();
            if (html != '')
                $('.top_top_top_login').html(html);
            return false;
        //$.fancybox(data);
        }
    });
//alert(data);
//$('div.top_top_login').html(data);
}

/*
function check(elem, msg){
    if(elem.val() == ''){
        elem.addClass('error').val(msg);
    }
    else 
    {                
        $.ajax(
        {
            type:'GET',
            url: base_url + 'soap/check_ticket/'+ elem.val(),
            data:'id_ticket='+ elem.val(),
            success: function(msg)
            {
                $('div#check_ticket').html(msg);
                $.fancybox([{'href': '#check_ticket'}], {
                    'transitionIn'        : 'fade',
                    'transitionOut'        : 'fade',
                    'centerOnScroll'    : 'true',
                    'overlayColor'        : '#000',
                    'overlayOpacity'    : '0.8',
                    'padding'            : '3',
                    'titlePosition'        : 'inside'
                });
            }
        });
        
        $('#check_ticket h3 span').text(elem.val());
        
        
    }
}
*/

function check(elem, msg){
    if(elem.val() == ''){
        elem.addClass('error').val(msg);
    }
    else 
    {        
        window.open(base_url + 'verifica/' + elem.val(), 'bilet', 'width=850,height=700,toolbar=yes, location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes, resizable=yes');               
        $('#check_ticket h3 span').text(elem.val());
    }
    
    
    return false;
}

function clear_msg(elem, msg){
	if(elem.value == msg){
		$(elem).removeClass('error');
		elem.value = '';
	}
}

// --------------------- fake select --------------------- //
function overLi(elem, on){
	if(on){
		elem.addClass('over');
	} else {
		elem.removeClass('over');
	}
}

function do_step_2(){

	$('table#which_sports tr td p, table.choose_sports tr td p, ul.bets li p').click(function(){
		$(this).toggleClass('like');
	});
	
	$('.select:not(.date)').each(function(){
		var parinte = $(this);
		var triggered = parinte.find('.to_open');
		parinte.find('a.trigger').add(parinte).click(function(){
			if(triggered.is(':hidden')){
				parinte.addClass('open');
				triggered.slideDown(100);
			} else {
				triggered.slideUp(100, function(){ parinte.removeClass('open'); });
			}
			return false;
		});
		
		triggered.find('li:not(.no)').each(function(){
			var asta = $(this);
			asta.bind('mouseover', function(){ overLi(asta, true); });
			asta.bind('mouseout', function(){ overLi(asta, false); });
			asta.click(function(event){
				if(!parinte.hasClass('select_single')){
					event.stopPropagation();
					if(!asta.hasClass('like')){
						asta.addClass('like').unbind('mouseout', overLi).unbind('mouseover', overLi);
					} else {
						asta.removeClass('like').bind('mouseover', overLi(asta, true)).bind('mouseout', overLi(asta, false));
					}
				} else {
					asta.removeClass('over');
					parinte.find('p span').text(asta.text()).attr("class", asta.attr("class"));
				}
			});
		});
	});
	
	var one_opened = false;
	$(document).click(function(event) {
		$('.to_open').each(function(){
			if($(this).css('display') == 'block'){
				one_opened = true;
			}
		});
		if(one_opened){
			if (!$(event.target).hasClass('trigger') && !$(event.target).parent().parent().parent().hasClass('select')){
				$('.select').each(function(){
					$(this).removeClass('open').find('.to_open').hide();
					one_opened = false;
				});
			}
		}
	});
}

// -------------------- Register validation -------------------- //
function register_go_2(){
	var go = true;
	if(gol($('#register_name')) || (!isText($('#register_name').val()))){
		$('#register_name').parent().find('.error').slideDown(200);
		go = false;
	} else {
		$('#register_name').parent().find('.error').hide();
	}
	
	if(gol($('#register_surname')) || (!isText($('#register_surname').val()))){
		$('#register_surname').parent().find('.error').slideDown(200);
		go = false;
	} else {
		$('#register_surname').parent().find('.error').hide();
	}
	
	if(gol($('#register_city')) || (!isText($('#register_city').val()))){
		$('#register_city').parent().find('.error').slideDown(200);
		go = false;
	} else {
		$('#register_city').parent().find('.error').hide();
	}
	
	if(gol($('#register_tel')) || (!isNumber($('#register_tel').val()))){
		$('#register_tel').parent().find('.error').slideDown(200);
		go = false;
	} else {
		$('#register_tel').parent().find('.error').hide();
	}
	
	if(gol($('#register_email')) || (!isEmail($('#register_email').val()))){
		$('#register_email').parent().find('.error').slideDown(200);
		go = false;
	} else {
		$('#register_email').parent().find('.error').hide();
	}
	
	if(go){
		// make a function to process the variables and call it here
		var step_2 = $('#register_step_2').html();
		$('#register').parent().addClass('register_step_2_fix_parent').find('#register').css('opacity', '0').html(step_2).animate({opacity: 1}, 300);
		do_step_2();
	}
	return false;
}

function register_go_3(){
	var selected = [];
	
	$('#register #which_sports p').add('#register .select ul li').each(function(){
		if($(this).hasClass('like')){
			selected.push($(this).text());
		}
	});
	
	alert(selected);
	return false;
}

function login_check(){
	var go = true;
	
	if(gol($('#username'))){
		$('#username').parent().find('.error').slideDown(200);
		go = false;
	} else {
		$('#username').parent().find('.error').hide();
	}
	
	if(gol($('#password'))){
		$('#password').parent().find('.error').slideDown(200);
		go = false;
	} else {
		$('#password').parent().find('.error').hide();
	}
	
	if(go){
		// do the login
	}
	return false;
}

$(function(){
	
	// ----------------------- preloading images ----------------------- //
	$.preloadCssImages();
    ticket_update_summary();
	
	
	// ----------------------- menu ----------------------- //
	$('ul.menu li:not(.on)').hover(function(){
		$(this).addClass('over');
		if(navigator.userAgent.indexOf('MSIE 6') != -1 && $(this).hasClass('first')){
			$(this).css('backgroundPosition', '-262px -200px');
		}
	}, function(){
		$(this).removeClass('over');
		if(navigator.userAgent.indexOf('MSIE 6') != -1 && $(this).hasClass('first')){
			$(this).css('backgroundPosition', '');
		}
	});
	
	$('ul.menu li:not(.on) a').hover(function(){
		$(this).parent().addClass('over');
		if(navigator.userAgent.indexOf('MSIE 6') != -1 && $(this).parent().hasClass('first')){
			$(this).parent().css('backgroundPosition', '-262px -200px');
		}
	},function(){
		$(this).parent().removeClass('over');
		if(navigator.userAgent.indexOf('MSIE 6') != -1 && $(this).parent().hasClass('first')){
			$(this).parent().css('backgroundPosition', '');
		}
	});
	
	
	// ----------------------- inside_menu ----------------------- //
	$('ul.inside_menu li:not(.on)').hover(function(){
		$(this).addClass('over');
		if(navigator.userAgent.indexOf('MSIE 6') != -1 && $(this).hasClass('first')){
			$(this).css('backgroundPosition', '-262px -200px');
		}
	}, function(){
		$(this).removeClass('over');
		if(navigator.userAgent.indexOf('MSIE 6') != -1 && $(this).hasClass('first')){
			$(this).css('backgroundPosition', '');
		}
	});
	
	$('ul.inside_menu li:not(.on) a').hover(function(){
		$(this).parent().addClass('over');
		if(navigator.userAgent.indexOf('MSIE 6') != -1 && $(this).parent().hasClass('first')){
			$(this).parent().css('backgroundPosition', '-262px -200px');
		}
	},function(){
		$(this).parent().removeClass('over');
		if(navigator.userAgent.indexOf('MSIE 6') != -1 && $(this).parent().hasClass('first')){
			$(this).parent().css('backgroundPosition', '');
		}
	});
	
	
	
	
	
	// ----------------------- home table hover ----------------------- //
	$('table.hover').each(function(){
		var parinte = $(this);
		parinte.find('tr:not(:eq(0))').not('.tr_offer').each(function(){
			var asta = $(this);            
            asta.hover(function(){
                asta.toggleClass('hover');
            });
            
            /*
			asta.click(function(){
				document.location.href = asta.find('.link').text();
			});
            */
		});
	});
	
	
	/*
	
	// ----------------------- home carousel ----------------------- //
	$('.home_carousel_container').each(function(){
		
		var amount = 980;
		var home_carousel = $(this);
		var home_carousel_nav_container = home_carousel.find('.home_carousel_nav_wrap');
		var home_carousel_nav = home_carousel.find('.home_carousel_nav');
		var home_carousel_cate = home_carousel.find('.home_carousel').children().length;
		var home_carousel_current = home_carousel.find('.home_carousel').children(":first");
		var navigation = '';
				
		
		
		home_carousel_current.css('left','0');
		for(i=home_carousel_cate;i>=1;i--){
			navigation += '<li><a href="#"><span>'+i+'</span></a></li>';
		};
		
		//home_carousel_cate++;
		
		//home_carousel_nav.width(home_carousel_nav.children(':first').outerWidth(true) * home_carousel_cate - parseFloat(home_carousel_nav.css('paddingLeft')) - parseFloat(home_carousel_nav.css('paddingRight')));
		home_carousel_nav.prepend(navigation);
		var new_width = home_carousel_nav.outerWidth(true);
		if (new_width < 185) new_width = 185; //min width ugly hack ...
		home_carousel_nav_container.width(new_width);
		var home_carousel_active = home_carousel_nav.children(":first").find('a');
		home_carousel_active.addClass('on');
		
		// Next button
		
		home_carousel_nav.find(".next").find('a').click(function(){
			home_carousel_current.animate({
				left: -amount+'px'
			},300, function(){
				$(this).css('left', amount+'px');
			});
			home_carousel_active.removeClass('on');
			if(home_carousel_current.next().index() == -1){
				home_carousel_current = home_carousel.find('.home_carousel').children(":first");
			} else {
				home_carousel_current = home_carousel_current.next();
			};
			home_carousel_active = home_carousel_nav.children(":eq("+home_carousel_current.index()+")").find('a');
			home_carousel_active.addClass('on');
			home_carousel_current.animate({
				left: 0+'px'
			},300);
			return false;
		});
		
		home_carousel_nav.find(".prev").find('a').click(function(){
			home_carousel_current.animate({
				left: amount+'px'
			},300);
			home_carousel_active.removeClass('on');
			if(home_carousel_current.prev().index() == -1){
				home_carousel_current = home_carousel.find('.home_carousel').children(":last");
			} else {
				home_carousel_current = home_carousel_current.prev();
			};
			home_carousel_active = home_carousel_nav.children(":eq("+home_carousel_current.index()+")").find('a');
			home_carousel_active.addClass('on');
			home_carousel_current.css('left','-'+amount+'px').animate({
				left: 0+'px'
			},300);
			return false;
		});
		
		home_carousel_nav.children(":not(.prev,.next)").find('a').click(function(){
			if(!$(this).hasClass('on')){
				var loc = $(this).parent().index();
				home_carousel_current.animate({
					left: -amount+'px'
				},300, function(){
					$(this).css('left', amount+'px');
				});
				home_carousel_current = home_carousel.find('.home_carousel').children(':eq('+loc+')');
				home_carousel_current.animate({
					left: 0+'px'
				},300);
				
				home_carousel_active.removeClass('on');
				home_carousel_active = $(this)
				home_carousel_active.addClass('on');
			};
			return false;
		});
		
	});
	
	*/
	
	// --------------------- scroll --------------------- //
	if($('#hotnews ul').html() != null){
		var theSpeed = 400;
		var pauseOnHover = true;
		var nextBtn = $('#hotnews .scroll_prev_next a.scroll_next');
		var prevBtn = $('#hotnews .scroll_prev_next a.scroll_prev');
		var timeToDelayOnClick = 5000; // in milliseconds
		$('#hotnews ul').infiniteScroll(theSpeed, pauseOnHover, nextBtn, prevBtn, timeToDelayOnClick);
	}
	
	
	// --------------------- register --------------------- //
	$('#launch_register, .fancybox, #gallery ul.the_gallery li a').fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'centerOnScroll'	: 'true',
		'overlayColor'		: '#000',
		'overlayOpacity'	: '0.8',
		'padding'			: '3',
		'autoScale'			: 'true',
		'titlePosition'		: 'inside',
		'autoDimensions'	: 'true'
	});
	
	
	// --------------------- close info --------------------- //
	$('.info a').click(function(){
		$('.info').slideUp(200);
		return false;
	});
	
	
	// --------------------- table img hover popup --------------------- //
	$('table.say tr td img').each(function(){
		var asta = $(this);
		var txt = asta.parent().text();
		var string = '<div class="say_this"><div class="say_this_middle"><div class="say_it">' + txt + '</div></div></div>';
		asta.parent().append(string);
		asta.hover(function(e){
			var difference = - parseFloat(asta.parent().find('.say_this').outerWidth(true))/2 + parseFloat(asta.outerWidth(true))/2 + asta.offset().left - asta.parent().offset().left - 3;
			if(navigator.userAgent.indexOf("MSIE 6") != "-1"){
				difference -= 10;
			}
			asta.parent().find('.say_this').css('left', difference + 'px').show();
		}, function(){
			asta.parent().find('.say_this').hide();
		});
	});
	
	
	// --------------------- echipa hover --------------------- //
	$('.echipa_a').each(function(i){
		var parinte = $(this);
		parinte.find('a').hover(function(){
			if(i<5){
				parinte.toggleClass('asta_ie6');
			} else {
				parinte.toggleClass('asta');
			}
		});
	});
	
	
	// --------------------- games --------------------- //
	if($("#games").html() != null){
		$("#games ul").circularCarousel(5, 5, 300, $('ul.games_nav li.prev'), $('ul.games_nav li.next'), false, 1, true);
	}
	
	if($("#games ul").children().length < 5){
		$('.games_wrap ul.games_nav').css('display', 'none');
	}
	
	
	$('#target ul li:first').show();
	// selecting the game when clicking on the thumbnail
	var current = 0;
	var target = $('#target ul');
	var cate = target.children().length;
	$('#games ul li').each(function(){
		var asta = $(this)
		asta.data('index', asta.index());
	});
	$('#games ul li').click(function(){
		var asta = $(this);
		if(current != asta.data('index')){
			target.children(':eq('+current+')').fadeOut(300);
			current = asta.data('index');
			target.children(':eq('+current+')').fadeIn(300);
		}
		return false;
	});
	
	// the random button
	target.parent().find('a.randomize').click(function(){
		var to_come = Math.floor(Math.random() * cate);
		if(target.children(':eq('+to_come+')').css('display') == 'none'){
			target.children(':eq('+current+')').fadeOut(300);
			current = to_come;
			target.children(':eq('+current+')').fadeIn(300);
		}
		return false;
	});
	
		
	// the thumbs
	if($("#gallery_thumbs .the_thumbs ul").html() != null){
		$("#gallery_thumbs .the_thumbs ul").circularCarousel(7, 7, 300, $('#gallery_thumbs ul.thumbs_nav li.prev'), $('#gallery_thumbs ul.thumbs_nav li.next'), false, 1, true);
	}
	
	if($("#gallery_thumbs .the_thumbs ul").children().length < 7){
		$('#gallery_thumbs ul.thumbs_nav').css('display', 'none');
	}
	
	// --------------------------------------------------- //
	
	
	// oferte
	$('#oferte ul li p.radio').each(function(){
		$(this).find('a').click(function(){
			var asta = $(this);
			var is = false;
			if(!asta.hasClass('on')){
				is = true;
			}
			$('#oferte ul li p.radio').each(function(){
				var this_one = $(this);
				if(this_one.find('a').hasClass('on')){
					this_one.find('a').removeClass('on');
					this_one.closest('li').find('.real_offer').slideUp(200, function(){ $('#oferte .like').removeClass('like'); });
				}
			});
			if(is){
				asta.toggleClass('on');
				asta.closest('li').find('.real_offer').slideToggle(200);
			}
			return false;
		});
	});
	
	
	// one_choice table
	$('table.one_choice').each(function(){
		var parinte = $(this);
		var asta = parinte.find('p');
		asta.click(function(){
			parinte.find('p').each(function(){
				if($(this).hasClass('like')){
					$(this).removeClass('like');
				}
			});
			$(this).addClass('like');
		});
	});
	
	do_step_2();
});



