// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
defaultEmail = 'ENTER YOUR EMAIL ADDRESS HERE:';


/* $(document).ready(function() {
	window.fbAsyncInit = function() {
		FB.init({
			appId: '207793359245940',
			status: true,
			cookie: true,
			xfbml: true
		});

		$('#logoff').click(function(e) {
			try {
				FB.logout(function(response) {});
			} catch(e) {}
			$.cookies.set('logout', '1', {domain: '.jdeal.com'});
			return true;
		});					

		FB.Event.subscribe('auth.sessionChange', function(response) {
			if (response.session) {
				// user logged in
			} else {
		    // no user session available, someone you dont know
				$.cookies.set('logout', '1', {domain: '.jdeal.com'});
				window.location.reload();
			}
		});

	};
	(function() {
		var e = document.createElement('script');
		e.async = true;
		e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
		document.getElementById('fb-root').appendChild(e);
	} ());

}); */

function OnRequestPermission() {
	var myPermissions = "publish_stream"; // permissions your app needs
	FB.Connect.showPermissionDialog(myPermissions, function(perms) {
		if (!perms) {
			// handles if the user rejects the request for permissions. This is a good place to log off from Facebook connect
			FB.logout(function(response) {
				// user is now logged out
			});
		} else {
			// finish up here if the user has accepted permission request
			window.location.reload();
		};
	});
}



function popup(mylink, windowname) {
	if (!window.focus) return true;
	var href;
	if (typeof(mylink) == 'string') href = mylink;
	else href = mylink.href;
	window.open(href, windowname, 'width=550,height=350,scrollbars=yes');
	return false;
}

function signin() {
	if($('iframe#chompon').size() > 0) {
		// chompon iframe here, this will work!
		showLoginBox();
		return false;
	} else {
		
		var today = new Date();
		 var expire = new Date();
		 expire.setTime(today.getTime() + 3600000*24*365*10);		
		document.cookie = "login=1;expires="+expire.toGMTString()+";path=/";		
		window.location.href = '/california';
		return false;
	}
	
	// $.cookies.set('login', '1', {domain: '.jdeal.com'});	
	// window.location.href = '/';	
}


function popupsignin() {
	

	var today = new Date();
	 var expire = new Date();
	 expire.setTime(today.getTime() + 3600000*24*365*10);		
	document.cookie = "login=1;expires="+expire.toGMTString()+";path=/";
   if($('iframe#chompon').size() > 0)	
	window.location.href = '/';
   else
	window.location.href = '/california';
	return false;
	
	
	// $.cookies.set('login', '1', {domain: '.jdeal.com'});	
	// window.location.href = '/';	
}

function showLoginBox() {
if (location.protocol === 'https:')
	var url="https://www.chompon.com/i_popups?pid=2816&popt=login&next=/i_deal%3Fnver%3D1%26pid%3D2816%26fg%3Dffffff%26wh%3D960%26ch_tmpl%3Ddogolf%26noacc%3D1%26login%3D1%26did%3D";
else
	var url="http://www.chompon.com/i_popups?pid=2816&popt=login&next=/i_deal%3Fnver%3D1%26pid%3D2816%26fg%3Dffffff%26wh%3D960%26ch_tmpl%3Ddogolf%26noacc%3D1%26login%3D1%26did%3D";
	OpeniFrameURL(url, 340, 180);
	window.setTimeout(function() {
		$('#fancybox-content>#fancybox-frame').parent().next().css('left', '-13px');
		$('#fancybox-content>#fancybox-frame').parent().next().css('top', '-13px');
	}, 500);	
}

function showEmailPopup() {
	$("#id_LoginLink").trigger('click');
	window.setTimeout(function() {
		$('#fancybox-content').next().css('left', '13px');
		$('#fancybox-content').next().css('top', '13px');
	}, 200);
}

function connect_facebook()
{
      $("#id_btn_fb_login").trigger("click");
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
/* function OpeniFrameURL(url, wh, ht) 
{
	if(url=='close') {$.fancybox.close(); return;}$("#chompon_popup").attr("href", url); $("#chompon_popup").trigger('click'); $("#fancybox-wrap").css({'width': (parseInt(wh)+20)+'px', 'height': (parseInt(ht)+20)+'px', 'left': ($(window).width()-parseInt(wh)-60)/2+'px'}); $("#fancybox-content").css({'width': wh+'px', 'height': ht+'px'});
}
 */
// public/javascripts/application.js
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  });
  return this;
};
