
/*Apply Events*/
$(function(){
	var selectedForm;
	$('.maillist').live('submit',function(){
		selectedForm = this;
		$(selectedForm).slideUp(400, maillistFeatureSave);
		return false;
	});
	function maillistFeatureSave(frm){
		var eml = $(selectedForm).find('.email').val();
		if(!eml){
			alert('Please enter an email address');
			$(selectedForm).slideDown(300);
		}else if(!validateEmail(eml)){
			alert('Incorrect email addres format')
			$(selectedForm).slideDown(300);
		}else{
			$.ajax({url: PUBLIC_BASE_URL+'/api/subscribe',type: "POST",data: {'email':eml},dataType: "html"});
			$(selectedForm).html('Thankyou for subscribing! As new offers arrive we will let you know.');
			$(selectedForm).slideDown(400);
		}
	}
	
	/* Slider 1 */
	if($('.slider .slides').length > 0){
		$('.slider .slides').cycle({ 
			fx:     'fade', 
			//speed:	200,
			prev:   '#prev', 
			next:   '#next', 
			pauseOnHover: true,
			//after:   onAfter, 
			timeout: 1000 
		});
		$('.slider .slides').hover(
			function() { $(this).cycle('pause');$(this).find('.frame').addClass('pause') },
			function() { $(this).cycle('resume');$(this).find('.frame').removeClass('pause') }
		);

		//$('.next').css({'opacity':.3});
		$('.next').hover(
			function(){$(this).animate({width:52,height:40,top:215},100)},
			function(){$(this).animate({width:32,height:32,top:220},100)}
		);		
		//$('.previous').css({'opacity':.3});
		$('.previous').hover(
			function(){$(this).animate({width:52,height:40,top:215},100)},
			function(){$(this).animate({width:32,height:32,top:220},100)}
		);
	}
	
	if($('#searchform')){
		$('#searchform').submit(function(){
			var kw = $('#searchfield').val();
			if(!kw){
				alert('Please enter a keyword');
			}else{
				document.location.replace(PUBLIC_BASE_URL+'/search/'+kw);
			}
			return false;
		});
	}
	if(("ul.dropdfown")){
		$("ul.dropdown li").hover(function(){    
			$(this).addClass("hover");
			
			$('ul:first',this).css('visibility', 'visible');    
			//$('ul:first',this).css({'top':0,'left':0,'display':'block'})
		},function(){    
			$(this).removeClass("hover");
			$('ul:first',this).css('visibility', 'hidden');    
		}); 
	}
	//$("ul.dropdown li:has(ul)").find("a:first").append(" &raquo; ");
	
	/*
	var defaultHeight = $('#mainHeader').height();
	$('#buttonBrowse').click(function(){
		if($('#mainHeader').height()==300){
			$('#mainHeader').animate({height:defaultHeight});
		}else{
			$('#mainHeader').animate({height:300});
		}
		return false;
	});*/

	/* Tracking */
	$('.OfferLink').live('click',function(){
		var self = this;
		$.post(PUBLIC_BASE_URL+'/track-click',{'url':$(self).attr('href')},function(){
			//console.log('Go to: '+$(self).attr('href'));
			//document.location = $(self).attr('href');
			//return true;
		});
		//return false;
		return true;
	});
	
	
	
	/* group offer */
	$(window).scroll(function(){		
		var distanceTop = 10;
		if  ($(window).scrollTop() > distanceTop){
			showGroupon();
		}else{				
			hideGroupon();
		}
	});
	$(document.body).click(showGroupon());		
	var xhrGroupon=null;
	var grouponOpened=false;
	var manualClose=false;
	function showGroupon(){		
		if(grouponOpened==false&&!$.cookie('gClose')){
			grouponOpened=true;
			if(xhrGroupon){
				xhrGroupon.abort();
			}
			xhrGroupon = $.post('getGroupon.php',{'sid':SID},function(r){
				$('#slidebox').animate({'right':'0px'},300);
				$('#slidebox').show();
				$('#slidboxContent').html(r);
			});
		}
	}
	$('#slidebox .close').bind('click',function(){hideGroupon();manualClose=true;$.cookie('gClose',1)});
	function hideGroupon(){			
		$('#slidebox').stop(true).animate({'right':'-450px'},100);
		grouponOpened=false;
	}
		
});
function visit(pid){$.post(PUBLIC_BASE_URL+'/track-visit',{'pid':pid});}
function validateEmail(elementValue){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(elementValue);
}
function subscribeMaillist(){}
jQuery.fn.center = function() {
	var container = $(window);
	var top = -this.height() / 2;
	var left = -this.width() / 2;
	return this.css('position', 'absolute').css({ 'margin-left': left + 'px', 'margin-top': top + 'px', 'left': '50%', 'top': '50%' });
}
