
// TODO: png fixes (.ie-fix-opacity)
// TODO: css browser selectors (check name conventions)
// TODO: rewrite image swaps to css
// TODO: userbar.js
// TODO: change vote_ajax to use POST method
// TODO: prefetch scissor icon and voting buttons

var RMN = {};

(function ($) {

	// global vars
	var site_url='http://localhost/ppweek/index.php';
	var noVotesToStore = [];
	var yesVotesToStore = [];
	var cookieJar = $.cookieJar('rmnVoteStorage');
	var yesVotes = cookieJar.get('yesVotes') || [];
	var noVotes = cookieJar.get('noVotes') || [];
	var lastSiteClicked;
	var tooltipText = '复制优惠码并访问该网站';
	var clip;

	// on dom load
	$(function () {

		// only load images as they appear within the browser's viewport
		$('div#major img').lazyload();

		// add hooks for any modal windows
		$('a.modal').facebox();

	});



	function processCoupons() {

		// create tooltip element for coupon code hovers
		$('.coupon:last').after('<div id="couponTooltip">'+tooltipText+'</div>');
		var couponTooltip = $('#couponTooltip');

		$('div.coupon').each(function () {

			// extract some vars we've embedded in the html
			var data = $(this).metadata({type:'elem', name:'script'});
			var couponId = data.couponId;
			var siteId = data.siteId;

			var yesButton = $(this).find('button.yesButton');
			var noButton = $(this).find('button.noButton');
			var comments = $(this).find('div.comments');
			var openCommentsButton = $(this).find('a.openComments');
			var closeCommentsButton = $(this).find('a.closeComments');

			var code = $(this).find('.code strong');

			if(code.length > 0) {


				$(this).mouseover(function(){

					if (clip) {
						clip.destroy();
					}

					clip = new HeroClipboard.Client();

					var pos = code.position();

					clip.setText(code.text());
					clip.setHandCursor(true);
					clip.glue(code.get(0));

					// when coupon code is clicked...
					clip.addEventListener('onComplete', function() {
						// pop window if we havent already for this site (merchant)
						code.css({
							textDecoration: 'line-through',
							color         : '#857F74'
						});
						couponTooltip.text('复制成功!');
						if (lastSiteClicked != siteId) {
							lastSiteClicked = siteId;
							var merchantWindow = window.open(site_url+'/out/'+couponId, 'merchantWindow');
							if(!merchantWindow) { // window open has failed (safari?)
								var lastSiteClickedCookied = cookieJar.get('lastSiteClicked');
								if (lastSiteClickedCookied != siteId) window.location = '/out/'+couponId;
								cookieJar.set('lastSiteClicked', siteId);
							}
						}
					});

					// when coupon code is hovered...
					clip.addEventListener('mouseOver', function() {
						if (lastSiteClicked != siteId) { // havent clicked this merchant yet
							couponTooltip.text(tooltipText);
						} else {
							couponTooltip.text('复制优惠码');
						}
						couponTooltip.css({
							left: pos.left + code.width() + 15,
							top: pos.top - 3,
							opacity: 0.8,
							display: 'none'
						}).fadeIn(200);
					});

					clip.addEventListener('mouseOut', function() {
						couponTooltip.fadeOut(200);
					});



				});

			} // end: if code

			// attach events to voting buttons
			yesButton.click(function (e) {
				e.preventDefault();
				castVote(siteId, couponId, 'Y');
			});
			noButton.click(function (e) {
				e.preventDefault();
				castVote(siteId, couponId, 'N');
			});

			// attach events to open/close comments
			if(openCommentsButton) openCommentsButton.click(function (e) {
				e.preventDefault();
				comments.slideToggle('slow');
			});
			if(closeCommentsButton) closeCommentsButton.click(function (e) {
				e.preventDefault();
				comments.slideToggle('slow');
			});

			// customise coupon view based on user's previous votes
			if(yesVotes && jQuery.inArray(couponId, yesVotes) >= 0) {
				$(this).addClass('valid');
				yesButton.css('visibility', 'hidden');
			}
			if(noVotes && jQuery.inArray(couponId, noVotes) >= 0) {
				$(this).hide();
				$(this).addClass('userHide');
			}


		}); // end for each coupon
	} // end function processCoupons

	function castVote(siteId, couponId, vote) {

		$.post('http://localhost/ppweek/index.php/post/pf', { pid: couponId, site: siteId, vote: vote } );
		var coupon = $('#c'+couponId);
		if(vote == 'Y') {
			coupon.addClass('valid');
			coupon.find('button.yesButton').css('visibility', 'hidden');
			coupon.find('td.stats').html('<span class="good">生效!</span>这么好的东西 <a id="tellAFriendLink" href="http://localhost/ppweek/index.php/tellfriend/'+couponId+'">赶快告诉朋友去 &raquo;</a>');
			//coupon.find('td.stats').html('<span class="good">Worked!</span> Like to say thanks? <a id="tellAFriendLink" href="/how-much-did-you-save.php?store='+siteId+'">Tell us what you saved &raquo;</a>');
			yesVotes.push(String(couponId));
			cookieJar.set('yesVotes', yesVotes);
		} else {
			coupon.slideUp('slow');
			noVotes.push(String(couponId));
			cookieJar.set('noVotes', noVotes);
		}

	}

	function changeSubmissionType(submissionType) {
		var fields_code = $('#fields_code');
		var fields_printable = $('#fields_printable');
		var fields_tip = $('#fields_tip');
		fields_code.css('display', 'none');
		fields_printable.css('display', 'none');
		fields_tip.css('display', 'none');
		if(submissionType == 'code') fields_code.css('display', 'block');
		if(submissionType == 'printable') fields_printable.css('display', 'block');
		if(submissionType == 'tip') fields_tip.css('display', 'block');
	}
	
	$("#guanzhu").click(function(){
	var guanzhu_user=$("#guanzhu_user").text();
    var beiguanzhu_user=$("#beiguanzhu_user").text();
    var type=$("#ggtype").text();
	$.post(site_url+"/home/guanzhu", { guanzhu_user:guanzhu_user,beiguanzhu_user:beiguanzhu_user,type:type } ,
	function(){
		if(type==1){
	$("#guanzhufou").css({ display : "none"}); 
	$("#yiguanzhu").text("提交成功！我已关注Ta").fadeIn("slow");
		}
		if(type==2){
			$("#guanzhufou").css({ display : "none"}); 
	$("#yiguanzhu").text("取消成功！").fadeIn("slow");
		}
	}
	
	);
	
});







	RMN.processCoupons = processCoupons;
	RMN.changeSubmissionType = changeSubmissionType;


})(jQuery);

