$(document).ready(function () {
	var COOKIE_NAME = 'cookie_warning';
	var headerFooterSize = 300;     // header + footer + paddings + margins + borders
	var minContentSize = 100;
	$("#logo").click( function() {
		self.parent.location = "http://www.xtube.com/index.php";
	});

	var filter_i_like = $("#filter_i_like").val();
	var filter_sess_gender = $("#filter_sess_gender").val();

        $("#radio-male,#like-male").click(function() {
		var className=$('#radio-male').attr('className');
		if(className == 'gender_radio-on') {
			$('#radio-male').attr({className:'gender_radio-off'});
			$('#like-male').css('color','#7b7b7b');
		}
		else {
			$('#radio-male').attr({className:'gender_radio-on'});
			$('#like-male').css('color','#f7f7f7');
		}
		setGender();
		return false;
        });
        $("#radio-female,#like-female").click(function() {
		var className=$('#radio-female').attr('className');
		if(className == 'gender_radio-on') {
			$('#radio-female').attr('className','gender_radio-off');
			$('#like-female').css('color','#7b7b7b');
		}
		else {
			$('#radio-female').attr('className','gender_radio-on');
			$('#like-female').css('color','#f7f7f7');
		}
		setGender();
                return false;
        });
	$("#gender-male-m,#gender-male-f,#switch-female").click(function() {
		$('#i-am-male').css('display','block');
		$('#i-am-female').css('display','none');
		setGender();
		return false;
	});
	$("#gender-female-m,#gender-female-m,#switch-male").click(function() {
		$('#i-am-male').css('display','none');
		$('#i-am-female').css('display','block');
		setGender();
		return false;
	});
	$("#share_off").click(function() {
		if($("#share_on_box").position().left <= -1000){
			var p = $(this).position();
			var X = p.left + 73 + 'px';
			var Y = p.top + 'px';
		}
		else{
			X = '-10000px';
			Y = '-10000px';
		}

		$("#share_on_box").css('left',X).css('top',Y);
		return false;
	});
        $(".share_code_btn, .share_code_btn_photo").click(function() {
		var new_Y = $(this).position().top + 30;
                if($("#share_on_box").position().left <= -1000 || $("#share_on_box").position().top != new_Y){
                        var p = $(this).position();
                        var X = p.left + 134 + 'px';
                        var Y = p.top + 30 + 'px';
                }
                else{
                        X = '-10000px';
                        Y = '-10000px';
                }
                
		$("#embed_on_box").css('left','-10000px');
                $("#share_on_box").css('left',X).css('top',Y);
                return false;
        });
        $(".embed_code_btn").click(function() {
		var new_Y = $(this).position().top + 30;
                if($("#embed_on_box").position().left <= -1000 || $("#embed_on_box").position().top != new_Y){
                        var p = $(this).position();
                        var X = p.left + 134 + 'px';
                        var Y = p.top + 30 + 'px';
                }
                else{
                        X = '-10000px';
                        Y = '-10000px';
                }
                
		$("#share_on_box").css('left','-10000px');
                $("#embed_on_box").css('left',X).css('top',Y);
                return false;
        });
	$("#share_box_submit").click(function() {
		$('#share_on').submit();
	});
	$("#embed_on_box_big,#embed_on_box_small").click(function() {
		$(this).select();
	});
	$("#mar_summary").click(function() {
		document.location.href='/my_account.php';
	});
	$("#mar_transactions").click(function() {
		document.location.href='/my_transactions.php';
	});
	$("#mar_points").click(function() {
		document.location.href='/my_point.php';
	});
	$("#mar_linking").click(function() {
		document.location.href='/linking_codes.php';
	});
	$("#mar_cancel").click(function() {
		document.location.href='http://www.xtube.com/cancelmembership.php';
	});
	$("#mar_remove").click(function() {
		document.location.href='/remove_my_account.php';
	});
	$("#mar_subscenter").click(function() {
		document.location.href='/subscription.php';
	});
	$("#mar_subsvideo").click(function() {
		document.location.href='/my_subscribed_videos.php';
	});
	$("#mar_subsphoto").click(function() {
		document.location.href='/my_subscribed_photos.php';
	});
	$("#mar_subsamateur").click(function() {
		document.location.href='/my_subscribed_amateurs.php';
	});
	$("#mar_subschannel").click(function() {
		document.location.href='/my_subscribed_paidchannels.php';
	});
	$("#mar_mysubscripe").click(function() {
		document.location.href='/my_subscribers.php';
	});
	$("#mar_freevideos").click(function() {
		document.location.href='/my_favorite_videos.php';
	});
	$("#mar_freephotos").click(function() {
		document.location.href='/my_favorite_sets.php';
	});
	$("#mar_amavideos").click(function() {
		document.location.href='/my_favorite_amateur_videos.php';
	});
	$("#mar_amaphotos").click(function() {
		document.location.href='/my_favorite_amateur_photos.php';
	});
	$("#mar_amateurs").click(function() {
		document.location.href='/my_favorite_amateurs.php';
	});
	$("#mar_favdvds").click(function() {
		document.location.href='/my_favorite_paidchannels.php';
	});
    $(".popup > ul").tabs();
	$('#signupBtn').click(function() { 
		var t=Number(new Date());
		$("#banner_gift_div").css("display","none");
		$.get("/ajaxCaptcha.php?t=" + t,function(rst) {
			var temp= rst.split(' ');
			var session_code = temp[0];
			var myhost= temp[1];
			var pic_url = temp[2];
			$("#session_code").val(session_code);
			$("#myhost").val(myhost);
			$("#code_img").attr("src","http://" + myhost + "/captcha_image.php?img=" + pic_url);
		})
		$.get("/includes/ajaxAssLog.php?t=" + t,{act: "signup"});
		$(".popup > ul.popup_menu").tabs('select', 1); 
		return false;
	});
	$('#main_login').click(function() { 
		var t=Number(new Date());
		$("#banner_gift_div").css("display","none");
		$.get("/includes/ajaxAssLog.php?t=" + t,{act: "login"});
		$(".popup > ul.popup_menu").tabs('select', 0); 
		return false;
	});
	$('#popup_login').click(function() { 
		var t=Number(new Date());
		$.get("/includes/ajaxAssLog.php?t=" + t,{act: "swtlogin"});
		return false;
	});
	$('#popup_not_member').click(function() { 
		var t=Number(new Date());
		$.get("/ajaxCaptcha.php?t=" + t,function(rst) {
			var temp= rst.split(' ');
			var	session_code = temp[0];
			var myhost= temp[1];
			var	pic_url = temp[2];

			$("#session_code").val(session_code);
			$("#myhost").val(myhost);
			$("#code_img").attr("src","http://" + myhost +"/captcha_image.php?img=" + pic_url);
		})
		$.get("/includes/ajaxAssLog.php?f=php&t=" + t,{act: "swtsignup"});
		return false;
	});
	$('#popup_login_php').click(function() { 
		var t=Number(new Date());
		$.get("/includes/ajaxAssLog.php?t=" + t,{act: "swtlogin_php"});
		return false;
	});
	$('#popup_not_member_php').click(function() { 
		var t=Number(new Date());
		$.get("/ajaxCaptcha.php?t=" + t,function(rst) {
			var temp= rst.split(' ');
			var	session_code = temp[0];
			var myhost= temp[1];
			var	pic_url = temp[2];

			$("#session_code").val(session_code);
			$("#myhost").val(myhost);
			$("#code_img").attr("src","http://" + myhost +"/captcha_image.php?img=" + pic_url);
		})
		$.get("/includes/ajaxAssLog.php?t=" + t,{act: "swtsignup_php"});
		return false;
	});
	$("#signin").click(function(){
		var user_id = $("#loginUserId").val();
		var password = $("#loginPassword").val();
		var url = $("#rightLoginUrl").val();
		if(url == null){
			url = $("#redirectUrl").val();
		}

		$.ajax({
			type: "POST",
			url: "/ajax_login_process.php",
			data: {user_id: user_id,password: password,url: url},
			dataType: "text",
			async: false,
			success: function(text) {
				text = unescape(text);
				if(text.charCodeAt(0) == 10){
					text = text.substr(2);
				}
                text = text.replace(/\+/g," ");
				if(text.substr(0,12) == 'Login Failed'){
					alert(text);
				} else if (text.substr(0,1) == '/' || text.substr(0,7) == 'http://') {
					document.location.href= text;
				} else if (text.substr(0,9) == 'function ') {
					var funcStr = text.substring(9);
					LOGGED_IN = true;
					eval(funcStr);
//					document.location.href= document.location.href;
				} else {
					document.location.href= text;
//					alert(text);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
						alert('Login Failed: ' + textStatus);
					}
		});
		return false;
	});
	$("#signup_now").click(function(){
		var email = $("#email").val();
		var user_id = $("#user_id").val();
		var password1 = $("#password1").val();
		var password2 = $("#password2").val();
		var sex_type = $("input[name='sex_type']:checked").val();
		var gender = $("input[name='gender']:checked").val();
		var certify_age = $("#certify_age").attr('checked')? 'yes':'no';
		var agree_to_terms = $("#agree_to_terms").attr('checked')? 'yes':'no';
		var random = $("#enter_code").val();
		var url = $("#rightSignupUrl").val();
		var session_code = $("#session_code").val();
		var myhost = $("#myhost").val();

		$.ajax({
			type: "POST",
			url: "/ajax_signup_process.php",
			data: {	
					email: email, 
					user_id: user_id, 
					password1: password1, 
					password2: password2, 
					sex_type: sex_type, 
					gender: gender, 
					certify_age: certify_age, 
					agree_to_terms: agree_to_terms, 
					random: random, 
					myhost: myhost, 
					url: url, 
					session_code: session_code 
					},
			dataType: "text",
			async: false,
			success: function(text) {
				text = unescape(text);
				if(text.charCodeAt(0) == 10){
					text = text.substr(2);
				}
				if(text.substr(0,14) == 'Signup Failed:'){
					if(text.indexOf('Verify Code') > 0) {
						var t=Number(new Date());
						$.get("/ajaxCaptcha.php?t=" + t,function(rst) {
							var temp= rst.split(' ');
							var	session_code = temp[0];
							var myhost= temp[1];
							var	pic_url = temp[2];

							$("#session_code").val(session_code);
							$("#myhost").val(myhost);
							$("#code_img").attr("src","http://" + myhost +"/captcha_image.php?img=" + pic_url);
						})
					}
					alert(text);
				} else if (text.substr(0,1) == '/') {
					document.location.href= text;
                    tb_remove();
                    $('#banner_gift_div').css('display','block');
                    $(".close_window").click();
				} else if (text.substr(0,9) == 'function ') {
					var funcStr = text.substring(9);
					LOGGED_IN = true;
					eval(funcStr);
				} else {
                    tb_remove();
                    $('#banner_gift_div').css('display','block');
                    $(".close_window").click();
					alert(text);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert('Signup Failed: ' + textStatus);
			}
		});
		return false;
	});
})


function getLoginRightIcon(url, user_id, img) {
	$("#main_login").click();
	$("#loginRightBlock,#signupRightBlock").load("/includes/ajaxFreeUserLoginRight.php?uid="+ user_id +"&img=" + img +"&url=" + url);
	$("#rightLoginUrl,#rightSignupUrl").val(url); 
	return false;
}
function onClickFuncIcon(url, user_id, img) {
		var rtn = checkLogin();
		if(rtn =="no") {
			getLoginRightIcon(url, user_id, img);
			return false;
		}
}

function checkLogin() {
	return $.ajax({
		type: 'GET',
		url: '/includes/ajaxAuth.php',
		dataType: 'text',
		async: false
	}).responseText;
}


