var Coupon = new Class({
	initialize: function( id, name, description, long_description, value, offer, newCoupon, local, french_image ){
		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.french_image = french_image;
		
		this.couponDiv = new Element('div', {
			'id': 'coupon_' + this.id,
			'class': 'coupon'									 
		});
		
		this.couponButton = new Element('div', {							   
			'class' : 'coupon_button'
		}).injectInside( this.couponDiv );
		
		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 );
		
		//Remove <sup> tags from alt text
		alt_str = cutString( this.offer + ' - ' + this.name, "<sup>", "</sup>" );
		alt_str = cutString( alt_str, "<sup>", "</sup>" );
		
		image_path = '/images/coupon_images/coupons_small/' + this.id + '.jpg';
		if ( this.french_image && this.local == 'fr_ca' ){
			image_path = '/images/coupon_images/coupons_small_fr/' + this.id + '.jpg';
		}		
		this.image = new Element( 'img', {
			'alt':alt_str,
			'src':image_path						
		}).injectInside( this.couponImage );
		
		this.couponDescription = new Element('div', {
			'class' : 'coupon_description',
			'html'  : this.description + '&nbsp;&nbsp;'
		}).injectInside( this.couponArea );
		
		this.info_button = new Element('img',{
			'class' : 'info_button',
			'width' : '11',
			'height': '9',
			'src' 	: '/images/coupon_images/plusinfo.gif'
		}).injectInside( this.couponDescription );

		this.couponShare = new Element('div', {
			'class' : 'coupon_share'
		}).injectInside( this.couponArea );
		
		this.twitterShare = new Element('img', {
			'class' : 'twitter_button',
			'src'	: '/images/coupon_images/btn_twitter.gif'
		}).injectInside( this.couponShare );
		
		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 );
		

	},
	
	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.local, coupon.id );
		});	
		this.twitterShare.addEvent('click', function(){
			twitterShare( coupon.local, coupon.value, coupon.name );
		});	
		
		// 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 );
		});
		this.couponDescription.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 HeroCoupon = new Class({
	Extends: Coupon,
	initialize: function(id, name, description, long_description, value, offer, newCoupon, local, french_image,number_coupons){
		this.parent( id, name, description, long_description, value, offer, newCoupon, local, french_image );
		if( number_coupons == 2){
			
			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' );
			couponLongDescription = new Element( 'div', {
				'class': 'coupon_long_description_featured',
				'html': this.long_description
			}).injectInside(this.couponArea);
			this.couponImage.set( 'class', 'coupon_image_featured');
			
			image_path = '/images/coupon_images/coupons_featured/'+this.id+'.jpg';
			if ( this.french_image && this.local == 'fr_ca' ){
				image_path = '/images/coupon_images/coupons_featured_fr/' + this.id + '.jpg';
			}
			this.image.set( {
				'src'	: image_path, 
				'width'	: '144',
				'height': '144'
			});
			this.couponOffer.set( 'class', 'coupon_offer_featured');
			this.couponButton.set( 'class', 'coupon_button_featured' )
			
			this.info_button.set( {
				'src'	: '/'+this.local+'/coupons/images/plusinfo.gif', 
				'width'	: '47',
				'height': '9'
			});
		} else if ( number_coupons == 1 ){ //hero coupon
			this.couponDiv.set( 'class', 'coupon_hero' );
			this.couponButton.set( 'class', 'coupon_button_hero' );
			this.couponTitle.setStyle('display','none');
			this.couponOffer.setStyle('display','none');
			this.couponDescription.setStyle('display','none');
			this.couponImage.set( 'class', 'coupon_image_hero');
			image_path = '/images/coupon_images/coupons_hero/'+this.id+'.jpg';
			if ( this.local == 'fr_ca' ){
				image_path = '/images/coupon_images/coupons_hero_fr/' + this.id + '.jpg';
			}
			this.image.set( {
				'src'	: image_path, 
				'width'	: '684',
				'height': '268'
			});
		}
		this.couponShare.setStyle('display','none');
	},
	addClickEvent: function( page ){
		var coupon = this;
		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 );
		});
		this.couponDescription.addEvent( 'click', function(){
			page.showOverlay( coupon );
		});

	},
	selectCoupon: function(){
		this.selected = true;
		if ( !this.couponDiv.hasClass('selected') ){
			this.couponDiv.addClass('selected');
		}
	},
	
	unSelectCoupon: function(){
		this.selected = false;
		if ( this.couponDiv.hasClass('selected') ){
			this.couponDiv.removeClass('selected');
		}
	}
});


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, coupon.french_image );
		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' );
		couponLongDescription = new Element( 'div', {
			'class': 'coupon_long_description_featured',
			'html': this.long_description
		}).injectInside(this.couponArea);
		this.couponImage.set( 'class', 'coupon_image_featured');
		
		image_path = '/images/coupon_images/coupons_featured/'+this.id+'.jpg';
		if ( this.french_image && this.local == 'fr_ca' ){
			image_path = '/images/coupon_images/coupons_featured_fr/' + this.id + '.jpg';
		}
		this.image.set( {
			'src'	: image_path, 
			'width'	: '144',
			'height': '144'
		});
		this.couponShare.set( 'class', 'coupon_share_featured');
		this.twitterShare.set( 'class', 'twitter_button_featured');
		this.facebookShare.set( 'class', 'facebook_button_featured');
		this.couponOffer.set( 'class', 'coupon_offer_featured');
		this.couponButton.set( 'class', 'coupon_button_featured' )
		
		this.info_button.set( {
			'src'	: '/'+this.local+'/coupons/images/plusinfo.gif', 
			'width'	: '47',
			'height': '9'
		});
	},
	
	addClickEvent: function( page ){
		var coupon = this;
		this.couponButton.removeEvents();
		this.couponButton.addEvent( 'click', function(){
			page.addRemoveSelectedCoupon( coupon.originalCoupon );
		});
		this.facebookShare.addEvent('click', function(){
			facebookShare( coupon.local, coupon.id )
		});	
		
		this.twitterShare.addEvent('click', function(){
			twitterShare( coupon.local, coupon.value, coupon.name );
		});	
		
		// Add overlay event
		this.couponImage.removeEvents();
		this.couponImage.addEvent( 'click', function(){
			page.showOverlay( coupon );
		});
		this.couponDescription.addEvent( 'click', function(){
			page.showOverlay( coupon );
		});

	}
});

var UnavailableCoupon = new Class({
	Extends: Coupon,
	initialize: function( id, name, description, long_description, value, available_date, offer, local, french_image ){
		this.parent( id, name, description, long_description, value, offer, false, local, french_image );
		this.couponButton.set('html', available_date );
		this.couponDiv.set('class', 'coupon unavailable');
		this.couponButton.setStyle( 'cursor', 'default' )
		if ( available_date.length >= 24 ) {
			this.couponButton.addClass('long');		
		}
		
		if ( available_date == 'Ordered on brandSAVER' ){	
			this.couponButton.setStyles({
				'font-size': '12px',
				'padding-left': '13px'
			});
		}
		if ( available_date == 'Command&eacute; sur carnetRABAIS' ){
			this.couponButton.setStyles({
				'font-size': '10px',
				'padding-left': '12px',
				'padding-top': '5px',
				'font-weight': 'bold'
			});
		}
		
		var shader = new Element( 'div', {
			'class': 'unavailable_mask'						 
		}).injectInside(this.couponDiv);
	},
	
	addClickEvent: function( page ){
		this.couponButton.removeEvents();
		this.couponButton.addEvent( 'click', function(){});
	}
});


