var Ani = {};
var $Z = 1;
var $Cart = new Array();
var $CartQty = 0;
var $ID;
var $FF = 0;
var $FFS = false;
var $CCP = false;
$(window).load(function () {
	Ani.Slider = true;
	
	$.get('/J/Cart/Check/', function(D) {
		D = $.parseJSON($C(D));
		if (D != null) {
			$CartQty = 0;
			$Cart = D;
			for (var X=0;X<$Cart.length;X++) {
				$CartQty += parseInt($Cart[X].Q);
			}
			InitCart();
		}
	});
	
	// Load First "Now Showing"
	if ($('#FeaturedFilms').is('div')) {
		FeaturedFilm($('#FeaturedFilms li:first-child').attr('id').split('-')[1]);
		$FF = 1;
		$('#FeaturedFilms li').click(function () {
			FeaturedFilm($(this).attr('id').split('-')[1]);
		});
	}
	
	// Rotate "Now Showing"
	$Now = setTimeout('ChangeNowShowing()',10000);
	
	$('.Slider,.SliderContent').click(function() {
		$FFS = true;
		clearTimeout($Now);
	}).hover(function() {
		clearTimeout($Now);
	}, function() {
		if (!$FFS) {
			$Now = setTimeout('ChangeNowShowing()',5000);
		}
	});


	// Make links for Youtube
	$('.YouTubeLink').click(function(event) {
		if ($('a',this).attr('href').substr(0,22) == 'http://www.youtube.com') {
			event.preventDefault();
			$YouTube($('a',this).attr('href'));
		}
	});
	
	// For Film Search
	$('#FilmSearch select').change(function() {
		$('#FilmSearch form').submit();
	});
	$('#FindaFilm select').change(function() {
		$('#FindaFilm img').click();
	});
	$('#FindaFilm img').click(function() {
		$.post('/J/Films/List',{Program:$('[name=Program]').val(),Country:$('[name=Country]').val(),Genre:$('[name=Genre]').val(),Director:$('[name=Director]').val(),Venue:$('[name=Venue]').val(),Date:$('[name=Date]').val()}, function(D) {
			D = $.parseJSON($C(D));
			$('#FindaFilmContent > div').html(D);
		});
	});
	
	$(".Slider > ul > li").click(function () {
		if ($(this).hasClass('Selected') == false && Ani.Slider == false) {
			Ani.Slider = true;
			var O = $(".Slider .Selected");
			O.removeClass('Selected');
			$(this).addClass('Selected');
			$(this).children('div').show();
			
			var OC = $(O).children('div').attr('ID');
			var NC = $(this).children('div').attr('ID');

			O.animate({height: '60px'}, 1000, function () {
				$(this).children('div').hide();
			});
		
			$(this).animate({height: '180px'}, 1000, function () {
				Ani.Slider = false;
			});
			$('#'+NC+'Content').css('zIndex',$Z++).animate({left: '0px'}, 1000, function () {
				$('#'+OC+'Content').css('left','-620px');
			});
			
			if ($('#FeaturedFilms',this).is('div')) {
				$('#FeaturedFilms').animate({scrollTop: $('.Showing').position().top}, 1000);
			}
			
			//$('#'+OC+'Content').animate({left: '620px'}, 1000, function () {
			//	$(this).css('left','-620px');
			//});
		}
	});
	
	$(".ContentMenu li").mouseover(function () {
		$(this).stop().animate({paddingLeft: '30px'}, 250);
	});
	$(".ContentMenu li").mouseout(function () {
		$(this).stop().animate({paddingLeft: '0px'}, 250);
	});	
});	

$('#Calendar').ready(function() {
	if ($('#Calendar').is('div')) {
		$('#Calendar').fullCalendar({
			allDaySlot:false,
			dayClick: function(date, allDay, jsEvent, view) {
				$('#Calendar').fullCalendar('gotoDate', date);
         	$('#Calendar').fullCalendar('changeView', 'basicDay');
			},
			header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,basicDay'
			},
			eventSources: [{url: '/J/Films/Calendar', type: 'POST'}]
			
		});
	}
});

$('#Sponsors').ready(function() {
	//$('#SponsorsPrev').css({top:($('#Sponsors').offset().top+35)+'px',left:($('#Sponsors').offset().left-15)+'px'});
	//$('#SponsorsNext').css({top:($('#Sponsors').offset().top+35)+'px',left:($('#Sponsors').offset().left+575)+'px'});
	$('#Sponsors').carousel('#SponsorsPrev', '#SponsorsNext');
	$('#SponsorsPrev, #SponsorsNext').click(function (event) {
		if(event.originalEvent) {
			window.clearInterval(SSID);
		}
	});
});

function ChangeNowShowing() {
	if ($('.Slider').is('div')) {
		$FF++;
		if (!$('#FeaturedFilms li:nth-child('+$FF+')').is('li')) {
			$FF = 1;
		}
		FeaturedFilm($('#FeaturedFilms li:nth-child('+$FF+')').attr('id').split('-')[1],1000);
		$('#FeaturedFilms').animate({scrollTop: ($FF-1)*23}, 1000);

		$Now = setTimeout('ChangeNowShowing()',10000);
	}
}
function SponsorsSlide() {
	$('#SponsorsNext').click();
}
var SSID = window.setInterval(SponsorsSlide, 4000);

jQuery.fn.carousel = function(previous, next, options){
    var sliderList = jQuery(this).children()[0];
    
    if (sliderList) {
        var increment = jQuery(sliderList).children().outerWidth("true"),
        elmnts = jQuery(sliderList).children(),
        numElmts = elmnts.length,
        sizeFirstElmnt = increment,
        shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
        firstElementOnViewPort = 1,
        isAnimating = false;
        
        for (i = 0; i < shownInViewport; i++) {
            jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
            jQuery(sliderList).append(jQuery(elmnts[i]).clone());
        }
        
        jQuery(previous).click(function(event){
            if (!isAnimating) {
                if (firstElementOnViewPort == 1) {
                    jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
                    firstElementOnViewPort = numElmts;
                }
                else {
                    firstElementOnViewPort--;
                }
                
                jQuery(sliderList).animate({
                    left: "+=" + increment,
                    y: 0,
                    queue: true
                }, "swing", function(){isAnimating = false;});
                isAnimating = true;
            }
            
        });
        
        jQuery(next).click(function(event){
            if (!isAnimating) {
                if (firstElementOnViewPort > numElmts) {
                    firstElementOnViewPort = 2;
                    jQuery(sliderList).css('left', "0px");
                }
                else {
                    firstElementOnViewPort++;
                }
                jQuery(sliderList).animate({
                    left: "-=" + increment,
                    y: 0,
                    queue: true
                }, "swing", function(){isAnimating = false;});
                isAnimating = true;
            }
        });
    }
};


function FeaturedFilm(ID,S) {
	if (!S) {
		var S = 300;
	}
	$('#FeaturedFilms li').removeClass('Showing');
	$('#FeaturedFilms #Showing-'+ID).addClass('Showing');
	
	$.get('/J/Films/Get/'+ID, function(D) {
		D = $.parseJSON($C(D));
		$('.SliderContent').append(
			$('<div>').attr('id','FeaturedFilmsContentNew').append(
				$('<img>').attr('src','/images/Showing/Film'+ID+'.jpg').error(function() {
					$(this).attr('src','/images/FeaturedPlaceholder.jpg')
				})
			).append(
				$('<div>').addClass("FeaturedFilmTrans").append(
					$('<ul>').append(
						$('<li>').html(D.Title)
					).append(
						$('<li>').html('Country of Origin: '+D.Country)
					).append(
						$('<li>').html('Year: '+D.Year)
					).append(
						$('<li>').html('Language: '+D.Language)
					).append(
						$('<li>').html(D.Synopsis.substr(0,80)+'...')
					).append(
						$('<li>').append(
							$('<ul>').append(
								$('<li>').html('<a href="/Film/'+D.FilmID+'/'+D.Title.replace(/ /g,'_')+'">More Info</a>').append(
									$('<img>').attr('src','/images/ButtonInfo.png')
								)
							).append(
								$('<li>').html('<a href="javascript:BuyTickets(\''+D.FilmID+'\')">Buy Tickets</a>').append(
									$('<img>').attr('src','/images/ButtonBuy.png')
								)
							).append(
								$('<li>').html('<a href="'+D.TrailerURL+'" target="_blank">View Trailer</a>').append(
									$('<img>').attr('src','/images/ButtonTrailer.png')
								).click(function(event) {
									if ($('a',this).attr('href').substr(0,22) == 'http://www.youtube.com') {
										event.preventDefault();
										$YouTube($('a',this).attr('href'));
									}
								})
							)
						)
					)		
				)
			)
		);

		$('#FeaturedFilmsContentNew').css('zIndex',$Z++).animate({left: '0px'}, S, function () {		
			Ani.Slider = false;
			$('#FeaturedFilmsContent').remove();
			$(this).attr('id','FeaturedFilmsContent');
			$('.FeaturedFilmTrans').delay(500).fadeIn('slow');
		});
		/*
		$('#FeaturedFilmsContent').animate({left: '660px'}, 300,function() {
			$(this).remove()
		});
		*/
	});
}

function BuyTickets(ID) {
	if (ID) {
		$.get('/J/Films/Showtimes/'+ID, function(D) {
			D = $.parseJSON($C(D));
			$('#TicketOrder').remove();
			$('body').append(
				$('<div>').attr('id','TicketOrder').append(
					$('<div>')
				).click(function(event) {
					if (event.eventPhase == '2') {
						$(this).remove();
					}
				})
			);
			if (D.Event.Type == 'Film') {
				$('#TicketOrder div').append(
					$('<img>').attr('src','/images/Films/Poster'+D.Event.FilmID+'.jpg').error(function() {
						$(this).attr('src','/images/Poster/PosterDefault.jpg');
					})
				).append(
					$('<h1>').html(D.Event.Title)
				).append(
					$('<h3>').html('Country of Origin: '+D.Event.Country)
				).append(
					$('<h3>').html('Year: '+D.Event.Year)
				).append(
					$('<h3>').html('Language: '+D.Event.Language)
				).append(
					$('<div>').html(D.Showtimes)
				).append(
					$('<div>').html('Click on the showtime you would like to purchase.')
				).append(
					$('<button>').html('Close').css('float','right').click(function(event) {
						$('#TicketOrder').remove();
					})
				);
			} else {
				$('#TicketOrder div').append(
					$('<img>').attr('src','/images/Events/Event'+D.Event.EventID+'.jpg').error(function() {
						$(this).attr('src','/images/Events/EventDefault.jpg');
					})
				).append(
					$('<h1>').html(D.Event.Title)
			
				).append(
					$('<div>').html(D.Showtimes)
				).append(
					$('<div>').html('Click on the time of the event you would like to attend.')
				).append(
					$('<button>').html('Close').css('float','right').click(function(event) {
						$('#TicketOrder').remove();
					})
				);
				
			}
				
		
		});
	}
}


function OrderTickets(ID) {
	if (ID) {
		$ID = ID;
		$.get('/J/Films/Showtime/'+ID, function(D) {
			D = $.parseJSON($C(D));
			
			if (D.Showtime.Type=='Film') {
				var Title = '<b>Film:</b> '+D.Event.Title;
			} else {
				var Title = '<b>Event:</b> '+D.Event.Title;
			}			
			$('#TicketOrder').remove();
			$('body').append(
				$('<div>').attr('id','TicketOrder').append(
					$('<div>').append(
						$('<ul>').append(
							$('<li>').html('<b>Theatre:</b> '+D.Showtime.Venue)
						).append(
							$('<li>').html('<b>Date:</b> '+D.Showtime.Date)
						).append(
							$('<li>').html(Title)
						).append(
							$('<li>').html('<b>Showtime:</b> '+D.Showtime.Time)
						)
					).append(
						$('<h1>').html('Order Details')
					).append(	
						$('<h2>').html('Number of Tickets')
					).append(
						$('<div>').attr('id','TicketSelect')
					).append(
						$('<input>').attr({type:'button',value:'Add to Cart'}).click(function() {
							AddCart();
						})
					).append(
						$('<input>').attr({type:'button',value:'Check Out'}).click(function() {
							AddCart(1);
						})
					).append(
						$('<input>').attr({type:'button',value:'Close'}).click(function() {
							$('#TicketOrder').remove();
						})
					)
				).click(function(event) {

					if (event.eventPhase == '2') {
						$(this).remove();
					}
				})
			);
			
			$('#TicketSelect').append(
				$('<div>').append(
					$('<select>').attr('name','General')
				).append(	
					$('<label>').html('<b>$'+D.Showtime.PriceGeneral+'</b> - General Admission')
				)
			).append(
				$('<div>').append(
					$('<select>').attr('name','Student')
				).append(	
					$('<label>').html('<b>$'+D.Showtime.PriceStudent+'</b> - Seniors and Students')
				)
			).append(
				$('<div>').append(
					$('<select>').attr('name','Members')
				).append(	
					$('<label>').html('<b>$'+D.Showtime.PriceMembers+'</b> - FLIFF Members')
				)
			).append(
				$('<div>').append(
					$('<select>').attr('name','Super')
				).append(	
					$('<label>').html('<b>$'+D.Showtime.PriceSuper+'</b> - Superstars')
				)
			)
			
			$('#TicketSelect select').each(function() {
				for (var X=0;X<=15;X++) {
					$(this).append(
						$('<option>').html(X)
					)
				}
			});
		});
	}
}

function InitCart() {
	if ($CartQty > 0) {
		if ($('.CartCount').is('div')) {
			$('.CartCount').html($CartQty);
		} else {
			$('#TopCart').append(
				$('<div>').addClass('CartCount').html($CartQty)
			);
			$('#SideCart > a').attr('href','https://fliff.com/Checkout')
			
			$('#SideCart').append(
				$('<ul>').append(
					$('<li>').html('Your Cart')
				).append(
					$('<li>').append(
						$('<div>').addClass('CartCount').html($CartQty)
					)
				).append(
					$('<li>').html('Check out')
				).click(function(event) {
					event.stopPropagation();
					window.location = 'https://fliff.com/Checkout';
				})
			);
		}
		if ($CartQty > 9) {
			$('.CartCount').css('width','20px');
		}
	} else {
		$('.CartCount').remove();
		$('#SideCart ul').remove();
		$('#SideCart > a').attr('href','/Films_and_Events')
	}
}
function AddCart(Checkout) {
	var Items = new Array();
	var Qty = 0;
	$('#TicketSelect select').each(function() {
		if ($(this).val() != '0') {
			
			var Item = {ID:$ID,T:$(this).attr('name'),Q:$(this).val()};
			Items.push(Item);
		}
		Qty += parseInt($(this).val());
	});
	if (Qty == 0) {
		// Nothing in Cart
	} else {
		//$('#TicketOrder div ul li:nth-child(3)').html();
		if (!Checkout) {
			$('.Main').append(
				$('<div>').attr('id','AddedCart').html('x'+Qty+' - '+$('#TicketOrder div ul li:nth-child(3)').html().replace('<b>Film:</b>','')+'<br><label>'+$('#TicketOrder div ul li:nth-child(2)').html()+'<br>'+$('#TicketOrder div ul li:nth-child(4)').html()+'</label>')
			);	
	
			setTimeout(function() {
				$('#AddedCart').animate({top:'-35',right:'10',width:'1',height:'1'},300,function() {
					$(this).remove();
				})
			},5000);

			$('#TicketOrder').remove();
			$CartQty += Qty;
	
			InitCart();
		}
		$.post('/J/Cart/Add/',{Cart:Items}, function(D) {
			D = $.parseJSON($C(D));	
			$Cart = D;
			if (Checkout) {
				window.location = 'https://fliff.com/Checkout';
			}
		});
	}
}

function DeleteCart(O,ID) {
	if (!ID) {
		var ID = $(O).attr('CartID');
	}

	if (ID) {
		$.post('/J/Cart/Delete/', {ID:ID.split('-')[1],T:ID.split('-')[0]}, function(D) {
			D = $.parseJSON($C(D));
			$CartQty = parseInt(D);
			InitCart();
			Checkout();
		})
	}
}
function UpdateCart() {

	var Items = {};
	
	$('#ShoppingCart input[type=text]').each(function() {
		Items[$(this).attr('name')] = $(this).val();
	});

	$.post('/J/Cart/Update/', {C:Items}, function(D) {
			D = $.parseJSON($C(D));
			$CartQty = parseInt(D);
			InitCart();
			Checkout();
		})
	
}
function Checkout() {
	$CCP = false;
	$('#Checkout').ready(function() {
		$('#ShoppingCart').remove();
		$('#Checkout').before(
			$('<div>').attr('id','ShoppingCart')
		);

		$.get('/J/Cart/View/', function(D) {
			D = $.parseJSON($C(D));

			$('#ShoppingCart').append(
				$('<table>').append(
					$('<tr>').append(
						$('<th>').html('Event')
					).append(
						$('<th>').html('Qty')
					).append(
						$('<th>').html('Price').css('width','55px')
					).append(
						$('<th>').html('Total').css('width','65px')
					).append(
						$('<th>')
					)
				)
			);
			
			var TOTAL = 0;
			var QTY = 0;
			var SERV = 0;
			for (var X=0;X<D.length;X++) {
				var Price = parseFloat(D[X].Price).Money();
				var RowTotal = (Price*parseInt(D[X].Q)).Money();
				if (D[X].T == 'General' || D[X].T == 'Student') {
					SERV += parseInt(D[X].Q);
				}
				if (D[X].T == 'Student') {
					D[X].T = 'Senior/Student';
				}
				TOTAL += parseFloat(RowTotal);
				QTY += parseInt(D[X].Q);
				$('#ShoppingCart table').append(
					$('<tr>').append(
						$('<td>').append(
							$('<a>').attr('href','/Film/'+D[X].FilmID+'/'+D[X].Event).append( 	
								$('<img>').attr('src','/images/Films/Poster'+D[X].FilmID+'.jpg').addClass('MiniPoster').error(function() {
									$(this).attr('src','/images/Films/PosterDefault.jpg');
								})
							)
						).append(
							$('<p>').html(D[X].Event+'<br><label>'+D[X].Date+' - '+D[X].T+'</label>')
						)
					).append(
						$('<td>').css({textAlign:'center'}).append(
							$('<input>').val(D[X].Q).attr({type:'text',name:D[X].T+'-'+D[X].ID})
						)
					).append(
						$('<td>').html('&#36;'+Price)
					).append(
						$('<td>').html('&#36;'+RowTotal)
					).append(
						$('<td>').append(
							$('<img>').attr({src:'/images/ButtonDelete.png',CartID:D[X].T+'-'+D[X].ID}).click(function() {
								DeleteCart(this);
							})
						)
					)
				)
			}
			if (SERV > 0) {
				TOTAL += SERV;
				$('#ShoppingCart table').append(
						$('<tr>').attr('id','ServiceCharge').append(
							$('<td>')
						).append(
							$('<td>').attr('colspan','2').html('Service Charge')
						).append(
							$('<td>').html('&#36;'+SERV.Money())
						).append(
							$('<td>')
						)
				
				)
			}
			if (QTY == 0) {
				$('#ShoppingCart').empty().append(
					$('<div>').html('There are no items in your shopping cart')
				);
				$('#CartBilling, #CartPayment, #CartCheckout').hide();
			} else if (TOTAL > 0) {
				$('#ShoppingCart table').append(
					$('<tfoot>').append(
						$('<tr>').append(
							$('<td>')
						).append(
							$('<td>').html('<input type="button" value="Update">').css('textAlign','center').click(function() {
								UpdateCart();
							})
						).append(
							$('<td>').html('Total')
						).append(
							$('<td>').attr('id','OrderTotal').html('&#36;'+TOTAL.Money())
						).append(
							$('<td>')
						)
					)
				)
			} else {
				$('#CartPayment').hide();
			}
		
			$('#Checkout [name=Member]').change(function() {
				if ($('#Checkout [name=Member]').is(':checked')) {
					$('#ServiceCharge').hide();
					$('#OrderTotal').html('&#36;'+(TOTAL-SERV).Money());
				} else {
					$('#ServiceCharge').show();
					$('#OrderTotal').html('&#36;'+TOTAL.Money());
				}
			});
		
			$('#Checkout input[type=button]').click(function() {
				if (!$CCP) {
					$CCP = true;
					$('#Checkout input[type=button]').val('Processing');
					var E = false;
				
					$('#Checkout input[type=text],#Checkout select').css({backgroundColor:'#fff',border:'1px solid #999'});
				
					if ($('#Checkout [name=FirstName]').val() == '') {
						E = true;
						$('#Checkout [name=FirstName]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					if ($('#Checkout [name=LastName]').val() == '') {
						E = true;
						$('#Checkout [name=LastName]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					if ($('#Checkout [name=Address]').val() == '') {
						E = true;
						$('#Checkout [name=Address]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					if ($('#Checkout [name=City]').val() == '') {
						E = true;
						$('#Checkout [name=City]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					if ($('#Checkout [name=State]').val() == '') {
						E = true;
						$('#Checkout [name=State]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					if ($('#Checkout [name=Zipcode]').val().length < 5) {
						E = true;
						$('#Checkout [name=Zipcode]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					if ($('#Checkout [name=Email]').val() == '') {
						E = true;
						$('#Checkout [name=Email]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					if ($('#Checkout [name=Phone]').val() == '') {
						E = true;
						$('#Checkout [name=Phone]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
				
					var CC = $('#Checkout [name=CCNum]').val().replace(' ','').replace('-','');
					$('#Checkout [name=CCNum]').val(CC);
				
					if (TOTAL > 0 && $('#Checkout [name=CCNum]').val().length < 15 || $('#Checkout [name=CCNum]').val().length > 16) {
						E = true;
						$('#Checkout [name=CCNum]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					if (TOTAL > 0 && $('#Checkout [name=CCcvv]').val().length < 3 || $('#Checkout [name=CCcvv]').val().length > 4) {
						E = true;
						$('#Checkout [name=CCcvv]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					if (TOTAL > 0 && $('#Checkout [name=Signature]').val() == '') {
						E = true;
						$('#Checkout [name=Signature]').css({backgroundColor:'#ffe',border:'1px solid #f00'});
					}
					
					if (!$('#Checkout [name=Agree]').is(':checked')) {
						E = true;
						$('#Checkout [name=Agree]').parent().css('color','#f00');
					}
				
					if (!E) {
						var R = {};
						R.Company = $('#Checkout [name=Company]').val();
						R.FirstName = $('#Checkout [name=FirstName]').val();
						R.LastName = $('#Checkout [name=LastName]').val();
						R.Address = $('#Checkout [name=Address]').val();
						R.Address2 = $('#Checkout [name=Address2]').val();
						R.City = $('#Checkout [name=City]').val();
						R.State = $('#Checkout [name=State]').val();
						R.Zipcode = $('#Checkout [name=Zipcode]').val();
						R.Email = $('#Checkout [name=Email]').val();
						R.Phone = $('#Checkout [name=Phone]').val();
						R.CCnum = $('#Checkout [name=CCNum]').val();
						R.CCexp = $('#Checkout [name=CCExpMonth]').val()+$('#Checkout [name=CCExpYear]').val();
						R.CCcvv = $('#Checkout [name=CCcvv]').val();
						R.Signature = $('#Checkout [name=Signature]').val();
						
						if ($('#Checkout [name=Signature]').is(':checked')) {
							R.Member = 'Y';
						} else {
							R.Member = 'N';
						}
				
						$.post('/J/Cart/Authorize/', R, function(D) {
							D = $.parseJSON($C(D));
					
							if (D == 'APPROVAL') {
								$CartQty = 0;
								InitCart();
								
								// Complete!!
								window.location = '/Order_Confirmation';
		
							
							
							
							} else {
								$('#CartPayment > h2:first-child').html(D).css('color','#f00');
								$('#Checkout input[type=button]').val('Complete Order');
								$CCP = false;
							}
						});
					
					} else {
						$('#Checkout input[type=button]').val('Complete Order!');
						
						if ($('#ErrorMess').is('div')) {
							$('#ErrorMess').fadeOut(300, function() {
								$('#ErrorMess').fadeIn(300);
							});
						} else {
							$('#CartCheckout [type="button"]').after(
								$('<div>').attr('id','ErrorMess').html('Please recheck the fields marked in red.')
							);
						}
						
						$CCP = false;
						
						
						
					}
					
				}
			});
			

			$CartQty = QTY;	
			InitCart();
		});
	});
}






function $YouTube(L) {
	var ID = L.split('watch?v=')[1];
	$('body').append(
		$('<div>').addClass('YouTube').append(
			$('<div>').append(
				$('<iframe>').attr('src','http://www.youtube.com/embed/'+ID+'/?hd=1&autoplay=1')
			)
		).click(function() {
			$(this).remove();
		})
	);
}















function $C(I) {I=I.replace(/\s/g,"");



    if(!(/^[a-z0-9\+\/\s]+\={0,2}$/i.test(I)) || I.length % 4 > 0){

        

    }   



    //local variables

    var D = K1.replace('=',''),

        C, P, N,

        i=0,

        R = [];



    I = I.replace(/=/g, "");



    while(i < I.length){



        C = D.indexOf(I.charAt(i));

        N = i % 4;



        switch(N){

            case 1:

                R.push(String.fromCharCode(P << 2 | C >> 4));

                break;

            case 2: 

                R.push(String.fromCharCode((P & 0x0f) << 4 | C >> 2));

                break;

            case 3: 

                R.push(String.fromCharCode((P & 3) << 6 | C));

                break;

        }



        P = C;

        i++;

    }

    return R.join("");

}
var K1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

Number.prototype.Money = function(c, d, t){
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "." : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};


