var webUrl = "http://www.emygold.com/";

function judgeCookie(){
	var isCookie = navigator.cookieEnabled ? true : false;
	return isCookie;
}

function topcheck(){
	var u = $("#uname");
	var p = $("#pword");
	if($.trim(u.val())==""){
		jAlert('Please Input Your Name !', 'EmyGold Tips:');
		u.focus();
		return false;
	}else if($.trim(p.val())==""){
		jAlert('Please Input Your Password !', 'EmyGold Tips:');
		p.focus();
		return false;
	}else{
		return true;	
	}
}

function checkemail(email){
		var str=email;
		var i=str.indexOf("@");
		var j=str.lastIndexOf(".");
        var k=str.length-1-j;
		if((i<1)||(i-j>1)||(j<1)||(k<0))  
		    return true;
		else
			return false;
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function showList(){
	$("#nav a:eq(7)").removeClass("qgl");
	$("#nav a:eq(7)").addClass("qg2");
	$(".qglcon").show();
}

function hideList(){
	$("#nav a:eq(7)").removeClass("qg2");
	$("#nav a:eq(7)").addClass("qgl");
	$(".qglcon").hide();
}

var Christmas = {
	setting: {startline:1, scrollto: 0, scrollduration:1000, fadeduration:[500, 100]},
	controlHTML: '<img src="http://www.emygold.com/img/gift.png" style="width:183px; height:179px" />', 
	controlattrs: {offsetx:20, offsety:20}, 
	anchorkeyword: '#top',

	state: {isvisible:false, shouldvisible:false},

	box:function(){
		$('.backgroundDiv').height($(document).height() + "px");
		$('.backgroundDiv').width($(document.body).outerWidth(true) + "px");
		$(".backgroundDiv").css({"opacity":"0.6"}).fadeIn('normal');
		pElement = $(".info");
						
		if (pElement.width() < $(window).width()) {
			var lPosition = (document.documentElement.offsetWidth - pElement.width()) / 2;
		} else {
			var lPosition = document.documentElement.scrollLeft + 5;
		}
			
		if (pElement.height() < $(window).height()) {
			var tPosition = document.documentElement.scrollTop + ($(window).height() - pElement.height()) / 2;
		} else {
			var tPosition = document.documentElement.scrollTop + 5;
		}
						
		var positions = {
			left: lPosition + "px",
			top: tPosition + "px"
		};
				
		pElement.css(positions).fadeIn('normal');	   
	},
	
	init:function(christData){
		jQuery(document).ready(function($){
			var christArr = christData.split("|");
			var domhtml = '<h2>You have won a '+christArr[0]+' USD coupon code.</h2><ul><li>Use if in the shopping cart to reduce the total shopping cost.</li><li>Need Total shopping cost over '+christArr[2]+' USD.</li><li>Expired 24:00 today server time.</li></ul><span>'+christArr[1]+'</span>';
			var mainobj = Christmas
			var iebrws = document.all
			mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest 
			mainobj.$body=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body')
			mainobj.$control=$('<div id="topcontrol">'+mainobj.controlHTML+'</div>')
					.css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', bottom:mainobj.controlattrs.offsety, right:mainobj.controlattrs.offsetx, opacity:100, cursor:'pointer'})
					.attr({title:'View Coupon Code'})
					.click(function(){mainobj.box();
												  $(".info").removeClass("cwarn").addClass("cinfo");
					$(".info div:eq(1)").html(domhtml);return false})
					.appendTo('body')
			if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') 
				mainobj.$control.css({width:mainobj.$control.width()}) 
			$('a[href="' + mainobj.anchorkeyword +'"]').click(function(){
				mainobj.box()
				return false
			})
		})
	}
}

$(function(){
		   
		   
	var url = document.location.href;
	if(judgeCookie()){
		if($.cookie("member_name") == null){
		$(".login").html(' <form onsubmit="return topcheck();" id="loginform" name="loginform" method="post" action="'+webUrl+'logincheck.php">Username:<input name="username" type="text" id="uname" value=""/>&nbsp;&nbsp;Password: <input type=password name="password" id="pword" value=""/><input type="submit" class="sign" value=""><input name="url" type="hidden" id="url" value="'+url+'"/>&nbsp;&nbsp;&nbsp;&nbsp;<a href="'+webUrl+'login.php">Register</a> | <a href="'+webUrl+'login.php">Lost password</a></form>');
		}else{
			$(".login").html('Hello ,<font color="#ff0000">'+$.cookie("member_name")+'</font> ! Welcome to EmyGold.com.<a href="'+webUrl+'logout.php?url='+url+'">Login out</a>');
		}
	}else{
		$(".login").html(' <form onsubmit="return topcheck();" id="loginform" name="loginform" method="post" action="'+webUrl+'logincheck.php">Username:<input name="username" type="text" id="uname" value=""/>&nbsp;&nbsp;Password: <input type=password name="password" id="pword" value=""/><input type="submit" class="sign" value="">&nbsp;&nbsp;&nbsp;&nbsp;<a href="'+webUrl+'login.php">Register</a> | <a href="'+webUrl+'login.php">Lost password</a></form>');
		alert('Your browser does not support cookie,Sign your convenience, Please open cookie!');
	}
	
	$("#nav a:eq(7)").hover(showList,hideList);
	$(".qglcon").hover(showList,hideList);
		   
		   
	var bodyhtml = '<div class="backgroundDiv"></div><div class="info"><div class="close"><img src="http://www.emygold.com/img/infoclose.gif" /></div><div class="cinfo"></div></div>';
	$("body").append(bodyhtml);
	var data = 'Christmas=getGift';
	$.ajax({url: "../Christmas.php",type: "POST",data: data,cache: true,success: function (Gift){
		if(Gift != "noExist"){Christmas.init(Gift);}}
	});
	
	$(".close").click(function(){ 
		$(".info").fadeOut("slow",  function(){   
			$(".info").css("display","none");   
			$(".backgroundDiv").css("display","none");   
		});    
	}); 
	
	$("#an").click(function(){
		Christmas.box();
		$(".info").removeClass("cwarn cinfo");
		$(".info div:eq(1)").show().css({align:"center"}).html("<img src='http://www.emygold.com/img/75.gif' id='cimage'/><div id='cloading'>Loading...</div>");
		var data = 'Christmas=happy';
        $.ajax({
                url: "../Christmas.php",		
                type: "POST",
                data: data,			
                cache: false,
                success: function (coupon){
                   if(coupon == "Exist"){
						$(".info div:eq(1)").html("");
						$(".info").removeClass("cinfo").addClass("cwarn");
					}else{
						var christArr = coupon.split("|");
						var domhtml = '<h2>You have won a '+christArr[0]+' USD coupon code.</h2><ul><li>Use if in the shopping cart to reduce the total shopping cost.</li><li>Need Total shopping cost over '+christArr[2]+' USD.</li><li>Expired 24:00 today server time.</li></ul><span>'+christArr[1]+'</span>';
						Christmas.box();
						$(".info").removeClass("cwarn").addClass("cinfo");
						$(".info div:eq(1)").html(domhtml);
					}
				}		
            });

	})
})



