var Coupon = new Class({
	initialize: function( id, name, description, long_description, value, offer, newCoupon, local ){
		this.id = id;
		this.name = name;
		this.description = description;
		this.long_description = long_description;
		this.offer = offer;
		this.value = value;
		this.selected = false;
		this.featuredCoupon = null;
		this.newCoupon = newCoupon;
		this.local = local;
		
		this.couponDiv = new Element('div', {
			'id': 'coupon_' + this.id,
			'class': 'coupon'									 
		});
		
		this.couponArea = new Element( 'div', {
			'class': 'coupon_area'							  
		}).injectInside( this.couponDiv );
		
		this.couponTitle = new Element('div', {
			'class': 'coupon_title',
			'html': this.name 
		}).injectInside( this.couponArea );
		
		this.couponImage = new Element('div', {
			'class': 'coupon_image'
		}).injectInside( this.couponArea );
		
		this.image = new Element( 'img', {
			'src': '/images/coupon_images/coupons_small/' + this.id + '.jpg',
			'width': '144',
			'height': '121',
			'alt'	: this.name
		}).injectInside( this.couponImage );
		
		this.couponDescription = new Element('div', {
			'class' : 'coupon_description',
			'html'  : this.description
		}).injectInside( this.couponArea );
	
		this.couponShare = new Element('div', {
			'class' : 'coupon_share'
		}).injectInside( this.couponArea );
		
		this.facebookShare = new Element('img', {
			'class' : 'facebook_button',
			'src'	: '/images/coupon_images/btn_facebook.gif'
		}).injectInside( this.couponShare );
		
		this.couponOffer = new Element('div', {
			'class' : 'coupon_offer',
			'html'  : this.offer
		}).injectInside( this.couponArea );
		
		this.couponButton = new Element('div', {
			'id'	: this.id,							   
			'class' : 'coupon_button'
		}).injectInside( this.couponDiv );
	},
	
	selectCoupon: function(){
		this.selected = true;
		if ( !this.couponDiv.hasClass('selected') ){
			this.couponDiv.addClass('selected');
		}
		if ( this.featuredCoupon != null ){
			this.featuredCoupon.selectCoupon();	
		}
	},
	
	unSelectCoupon: function(){
		this.selected = false;
		if ( this.couponDiv.hasClass('selected') ){
			this.couponDiv.removeClass('selected');
		}
		if ( this.featuredCoupon != null ){
			this.featuredCoupon.unSelectCoupon();	
		}
	},
	
	addClickEvent: function( page ){
		var coupon = this;
		this.facebookShare.addEvent('click', function(){
			facebookShare( coupon.id );
		});	
		// Add select event
		this.couponButton.removeEvents();
		this.couponButton.addEvent( 'click', function(){
			page.addRemoveSelectedCoupon( coupon );
		});
		// Add overlay event
		this.couponImage.removeEvents();
		this.couponImage.addEvent( 'click', function(){
			page.showOverlay( coupon );
		});
		
		if ( this.featuredCoupon != null ){
			this.featuredCoupon.addClickEvent( page );
		} 
	},
	
	addFeaturedCoupon: function( featuredCoupon ){
		this.featuredCoupon = featuredCoupon;
	},
	
	getIEBrowserVersion : function()
	{
		var appVer = navigator.appVersion.toLowerCase();
		var iePos  = appVer.indexOf( 'msie' );
		
		if( appVer.indexOf( 'msie' ) != -1 )
		{
			var is_minor = parseFloat( appVer.substring( iePos + 5, appVer.indexOf( ';', iePos ) ) );
			var version  = parseInt( is_minor );
		}
		
		if( navigator.appName.substring( 0,9 ) == 'Microsoft' )
		{
			return version;
		}
		else
		{
			return false;
		} 	
	}
	
});

var FeaturedCoupon = new Class({
	Extends: Coupon,
	initialize: function( coupon ){
		this.parent( coupon.id, coupon.name, coupon.description, coupon.long_description, coupon.value, coupon.offer, coupon.newCoupon, coupon.local );
		this.originalCoupon = coupon;
		this.couponDiv.set( 'class', 'coupon_featured' );
		if ( this.newCoupon == true ){
			this.couponDiv.addClass('new');
		}
		this.couponArea.set( 'class', 'coupon_area_featured' );	
		this.couponTitle.set( 'class', 'coupon_title_featured' );
		this.couponTitle.set( 'html', this.name.toUpperCase() );
		this.couponDescription.set( 'class', 'coupon_description_featured' );
		
		if ( this.long_description == 'image' ){
			this.long_description = '<img alt ="slogan" src="/images/coupon_images/slogans_'+this.local+'/'+this.id+'.jpg"';
		}
		couponLongDescription = new Element( 'div', {
			'class': 'coupon_long_description_featured',
			'html': this.long_description
		}).injectInside(this.couponArea);
		
		this.image.set( 'class', 'coupon_image_featured');
		this.image.set( 'width', '180' );
		this.image.set( 'height', '180' );
		this.image.set( 'src', '/images/coupon_images/coupons_featured/'+this.id+'.png' );
		
		this.couponShare.set( 'class', 'coupon_share_featured');
		this.facebookShare.set( 'class', 'facebook_button_featured');		
		this.couponOffer.set( 'class', 'coupon_offer_featured');
		this.couponButton.set( 'class', 'coupon_button_featured' );
	},
	
	addClickEvent: function( page ){
		var coupon = this;
		this.facebookShare.removeEvents();
		this.facebookShare.addEvent('click', function(){
			facebookShare( coupon.id );
		});	
		this.couponButton.removeEvents();
		this.couponButton.addEvent( 'click', function(){
			page.addRemoveSelectedCoupon( coupon.originalCoupon );
		});
		
		// Add overlay event
		this.couponImage.removeEvents();
		this.couponImage.addEvent( 'click', function(){
			page.showOverlay( coupon );
		});

	}
});

var UnavailableCoupon = new Class({
	Extends: Coupon,
	initialize: function( id, name, description, long_description, value, available_date, offer ){
		this.parent( id, name, description, long_description, value, offer );
		this.couponButton.set('html', available_date );
		if ( available_date.length >= 30 ) {
			this.couponButton.addClass('long');		
		}
		this.couponButton.setStyle('cursor', 'default');
		this.couponDiv.set('class', 'coupon unavailable');
		
		var shader = new Element( 'div', {
			'class': 'unavailable_mask'						 
		}).injectInside(this.couponDiv);
	},
	
	addClickEvent: function( page ){
		this.couponButton.removeEvents();
		this.couponButton.addEvent( 'click', function(){});
	}
});


