$(document).ready(function() {

	var baseUrl = 'http://localhost/1045-HerbsofGold/05-Production/http/'
	
	
	
					function TwitterDateConverter(time){
						var date = new Date(time),
							diff = (((new Date()).getTime() - date.getTime()) / 1000),
							day_diff = Math.floor(diff / 86400);

						if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
							return;

						return day_diff == 0 && (
								diff < 60 && "just now" ||
								diff < 120 && "1 minute ago" ||
								diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
								diff < 7200 && "1 hour ago" ||
								diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
							day_diff == 1 && "Yesterday" ||
							day_diff < 7 && day_diff + " days ago" ||
							day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
					}
			
			
	
	
		/* --- Fix those form elements --- */
		
	//	if ($.browser.webkit) { $input.css('marginTop', 1); }
		
		
		
		/* --- Start slideshow --- */
		
		
		t = setTimeout ( "slideshow()", 9000 );

			var homehover = false
			$('#homeHero').hover(function() {

			homehover = true

			$('#homeHero #fancy').fadeIn(500)	

			}, function() {
				homehover = false
			// wait a bit then fade out. Make sure mouse has not returned to HomeHero	
				var ft = setTimeout ( fadeOutFancy, 1200 ); 			
			})

			function fadeOutFancy() {
				//alert('fired')
					if (!homehover) {
						$('#homeHero #fancy').fadeOut(500)
					}
			}

				$('#homeHero #fancy .middle a').click( function() {

					
					var activeImg = $('#homeHero .activeImg')
					var index = $(this).index()
					var loadImg = $('#homeHero div:eq('+index+')')
					
					
						var activeBtn = $('#fancy .middle a.active')
						var loadBtn = $('#fancy .middle a:eq('+index+')')
						
						$(activeBtn).removeClass('active') 
						$(activeBtn).addClass('inactive')
						
						$(loadBtn).removeClass('inactive') 
						$(loadBtn).addClass('active') 
						
					

					
					
					
					$(activeImg).fadeOut(1000)	
					$(activeImg).removeClass('activeImg')
					
					$(loadImg).addClass('loadingImg')
					$('#homeHero .loadingImg').fadeIn(1000)
					$('#homeHero .loadingImg').addClass('activeImg')
					$('#homeHero .activeImg').removeClass('loadingImg')
					
					
					clearInterval(t)
					t = setTimeout ( "slideshow()", 9000 );
					return false;
					
				})



				$('#homeHero #fancy a.left, #homeHero #fancy a.right').click( function() {

					if ($(this).attr('class') != 'pause') {


						if ($(this).attr('class') == 'right') {
							var activeImg = $('#homeHero .activeImg')


							if ($(activeImg).next('div.slide').length != 0) {
								$(activeImg).next('div.slide').addClass('loadingImg') 
								} else {
									$('#homeHero .slide:first').addClass('loadingImg') 
								}


								var activeBtn = $('#homeHero .middle a.active')
									if ($(activeBtn).next('a.inactive').length != 0) {
										$(activeBtn).next('a').removeClass('inactive') 
										$(activeBtn).next('a').addClass('active') 
										} else {
											$('#homeHero .middle a:first').removeClass('inactive') 
											$('#homeHero .middle a:first').addClass('active') 

										}
									$(activeBtn).removeClass('active')
									$(activeBtn).addClass('inactive')


						} else {

								var activeImg = $('#homeHero .activeImg')
								if ($(activeImg).prev().length != 0) {
									$(activeImg).prev().addClass('loadingImg') 
									} else {
										$('#homeHero .slide:last').addClass('loadingImg') 
									}

										var activeBtn = $('#homeHero .middle a.active')
											if ($(activeBtn).prev('a.inactive').length != 0) {
												$(activeBtn).prev('a').removeClass('inactive') 
												$(activeBtn).prev('a').addClass('active') 
												} else {
													$('#homeHero .middle a:last').removeClass('inactive') 
													$('#homeHero .middle a:last').addClass('active') 

												}
												$(activeBtn).removeClass('active')
												$(activeBtn).addClass('inactive')		


						}
						$(activeImg).fadeOut(1000)	
						$(activeImg).removeClass('activeImg')
						$('#homeHero .loadingImg').fadeIn(1000)
						$('#homeHero .loadingImg').addClass('activeImg')
						$('#homeHero .activeImg').removeClass('loadingImg')	


					}

					clearInterval(t)
						t = setTimeout ( "slideshow()", 9000 );
					return false;
				})
		
		
		/* --- End slideshow --- */
		
		
		
		/* ---- Home buttons ----*/
		
		$('#homeLinks div').css('cursor','pointer')
		$('#homeLinks div.products').click(function() {
			window.location = $(this).attr('title')
		})
		
		
			$('#homeLinks div.ask').fancybox({
				'padding'			: 0,
				'margin'			:0,
				'width'				: 860,
				'height'			: 560,
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'overlayColor'		: '#000',
				'overlayOpacity'	: 0.5,
				'type'				: 'iframe',
				'href'				: $('#homeLinks div.ask').attr('title')
			});
		
		
				$('a.askanaturopath').fancybox({
					'padding'			: 0,
					'margin'			:0,
					'width'				: 860,
					'height'			: 560,
					'autoScale'			: false,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'overlayColor'		: '#000',
					'overlayOpacity'	: 0.5,
					'type'				: 'iframe'
				
				});
		
			$('#homeLinks div.informed').fancybox({
				'padding'			: 0,
				'margin'			:0,
				'width'				: 860,
				'height'			: 560,
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'overlayColor'		: '#000',
				'overlayOpacity'	: 0.5,
				'type'				: 'iframe',
				'href'				: $('#homeLinks div.informed').attr('title')
			});
		
				$('a.subscribe').fancybox({
					'padding'			: 0,
					'margin'			:0,
					'width'				: 860,
					'height'			: 560,
					'autoScale'			: false,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'overlayColor'		: '#000',
					'overlayOpacity'	: 0.5,
					'type'				: 'iframe'
				});
				
				$('a.storelocator').fancybox({
					'padding'			: 20,
					'margin'			:0,
					'width'				: 860,
					'height'			: 560,
					'autoScale'			: false,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'overlayColor'		: '#000',
					'overlayOpacity'	: 0.5,
					'type'				: 'iframe'
				});
		
		

		function is_int(value){
		  if((parseFloat(value) == parseInt(value)) && !isNaN(parseInt(value))){
		      return true;
		 } else {
		      return false;
		 }
		}	
		
		
		
			if ($('div.ingredients').length != 0) {
			
				$('div.ingredients table').addClass('stripe')
				$('div.ingredients p:first').addClass('tableHead')
			//	$('div.ingredients table tr:first').insertBefore('<tr><th colspan="2">Each contains</th></tr>')
				$('div.ingredients table tr:first td:first').attr('width','400')
				$('div.ingredients table tr').each( function() {   
					$(this).children('td:eq(1)').css('text-align','right')
					})
			}
			
			if ($('div.indications').length != 0) {
			
			}
		
		
		/* ---- Store Locator ---- */
		
		if ($('#storefindersubmit').length != 0) {
		storefinderurl = $('#storefinderurl').val()
		
				$('#findastore').submit( function() {
					
							$('#storefindersubmit').fancybox({
								'width'				: 860,
								'height'			: 560,
								'padding'			: 20,
								'autoScale'			: false,
								'transitionIn'		: 'none',
								'transitionOut'		: 'none',
								'overlayColor'		: '#000',
								'overlayOpacity'	: 0.5,
								'type'				: 'iframe',
								'onClosed': function() {
								parent.location.reload(true);
								},
								'href'				: $('#storefinderurl').val() + "?q="+ $('#postcode').val()
							});
							$('#storefindersubmit').click()	
							
				return false	
				})
			
			
		
				
			
		}
		
		
		$('#links a.store').fancybox({
					'width'				: 860,
					'height'			: 560,
					'autoScale'			: false,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'overlayColor'		: '#000',
					'overlayOpacity'	: 0.5,
					'type'				: 'iframe'
			});
			
			
				$('a.forgotten').fancybox({
							'width'				: 560,
							'height'			: 360,
							'autoScale'			: false,
							'transitionIn'		: 'none',
							'transitionOut'		: 'none',
							'overlayColor'		: '#000',
							'overlayOpacity'	: 0.5,
							'type'				: 'iframe'
					});

		/* ---- Conditions rating ---- */
		
		
		if ($('#rater').length != 0) {
			var rated = false
			
			if ($('#rated').val() == 1) {
				rated = true
			}
			
			var origclass = $('.starsCont').attr('class')
			origclass = origclass.replace('starsCont','')
			var origindex = $('span#rating').html()
			
			$('#rater a').hover( function() {	
				
					if (!rated) {		
				var starindex = $(this).index()
				var displayindex = (starindex+1)
				var oldclass = $(this).parent().attr('class')
				var newclass = ''
				classes = new Array()
				classes[0] = 'one'
				classes[1] = 'two'
				classes[2] = 'three'
				classes[3] = 'four'
				classes[4] = 'five'
				oldclass = oldclass.replace('starsCont','')
				newclass = classes[starindex]
				$(this).parent().removeClass(oldclass)
				$(this).parent().addClass(newclass)
				$('span#rating').html(displayindex+'/5 stars')
				}
				
			})
			
			$('.starsCont').hover( function() {
				
			}, function() {
				if (!rated) {
					var currclass = $('.starsCont').attr('class')
					currclass = currclass.replace('starsCont','')
					$(this).removeClass(currclass)
					$(this).addClass(origclass)	
					$('span#rating').html(origindex)
				}
			})
			
			$('.starsCont a').click( function() {
				
				if (!rated) {
					var rating = $(this).index()
					rating = (rating+1)
					var sessid = $('#sess_id').val()
					var conditionid = $('#condition_id').val()
			
				$.ajax({
				   type: "POST",
				   url: baseUrl+"conditions/rate",
				   data: "r="+rating+"&s="+sessid+"&c="+conditionid,
				   success: function(msg){
						if (msg == 1) {
							rated = true;
							$('span#rating').html('Thank you!')
							$('#rater').parent().next('.foot').html('You rated this '+rating+' stars')
						} else {
						alert('error: '+msg)
						}
				   }
			
				 });
				}
				return false;
			})
			
			
		}
		
		
		/* ---- Twitter ---- */
		
		if ($('#tweets').length!=0) {
			
		$('.happyDate').each( function() {
			
			$(this).html(TwitterDateConverter($(this).html()))
			
		})
		
		$('#tweets .dots a').last().addClass('active')
		
		var index = 0;
		var indexMax = $('#tweets .dots a').length
		var disab = false
			$('#tweets .dots a').click( function() {
				index = ((indexMax-1)-$(this).index())
				
				if ($(this).hasClass('active') || disab) {
					return
				} else {
					$('#tweets .dots a').removeClass('active')
					$(this).addClass('active')
					disab = true
					
				}
				
					$('#tweets p.active').fadeOut(300, function() {
						$('#tweets p.active').removeClass('active')
						$('#tweets p:eq('+index+')').fadeIn(300, function() {
							$(this).addClass('active')
							disab = false
							return
						})
					})
			})
		


			
		}
		
		
		/* ---- Products image enlargement ---- */
		
		
		
		if ($('#productSummary').length != 0) {
			
			$('a.enlarger').fancybox({
		
				'overlayColor'		: '#000',
				'overlayOpacity'	: 0.5
			})
			
			$('div.image a').hover( function() {
				
				$(this).parent().children('div.helper').show()
				
			}, function() {
			
				$(this).parent().children('div.helper').hide()
				})
		
		}
		
		
		/* ---- Products dynamic pricing ---- */
		
		$('select.productSize').change( function() {
		
			// Get the product ID from the ID
			var updateId = $(this).attr('id')
			var priceInfo = $(this).children('option:selected').attr('title')
			var priceArray = priceInfo.split('|')
			updateId = updateId.substr(1)
			updateId = 'dyn'+updateId
			$('#'+updateId).children('span.sizer').html( priceArray[0]+'<br/>')
			$('#'+updateId).children('span.price').html( 'A$'+priceArray[1])
			
		})
		
		/* ---- Categories image hover to select sibling link ---- */
		
		if ($('#categories').length != 0) {
			
			$('.item').hover( function() {
				
				if ( !$(this).hasClass('blank')) {
				
				$(this).children('.words').children('h4').children('a').addClass('hoverClass')
				$(this).css('cursor','pointer')
				$(this).children('.words').addClass('itemHover')
				}
			}, function() {	
					if ( !$(this).hasClass('blank')) {
				
					$(this).children('.words').removeClass('itemHover')
						$(this).children('.words').children('h4').children('a').removeClass('hoverClass')
					}
					
					
					})
					
					
			
				$('.item').click( function() {
					
					var gotourl = $(this).children('.words').children('h4').children('a').attr('href')
					window.location = gotourl;
				})
			
		}
		
		/* ---- Products listing ---- */
		
			if ($('#listing').length != 0) {
			
					$('.productBrief').hover( function() {
					//	$(this).addClass('productBriefHover')
					//	$(this).children('div.main').children('div.words').children('h3').children('a').addClass('hoverClass')
					}, function() {	
					//	$(this).removeClass('productBriefHover')
							
							})
				
			}
		
		
		
		/*  --- Table stripes --- */
		
		$('table.stripe').children().children('tr:odd').addClass('odd');

		$("#categoryJump").change(function() {
			params = ($('#categoryJump').val());
			window.location = params; 
		});
		$("#sortJump").change(function() {
			params = ($('#sortJump').val());
			window.location = params; 
		});
		
		
		
		

		
			$('.headRow .tabs a').click( function() {

			if ($(this).attr('class') == 'inactive') {
				$('.headRow .tabs a').addClass('inactive')
				$(this).removeClass('inactive')

				addDiv = $(this).attr('rel')

				$(this).parent('div').parent('div').next('div').children('div.active').hide()
				$(this).parent('div').parent('div').next('div').children('div.scrollCont').hide()
				$(this).parent('div').parent('div').next('div').removeClass('active')
				$('#'+addDiv).parent('div').children('div.scrollCont').show()
				$('#'+addDiv).show()
				$('#'+addDiv).addClass('active')
			}

			return false

			})



				if ($('#article').length != 0) {
					($('.body p:first').addClass("intro"))
				}

					$('#breakout').insertAfter($('.body p:eq(2)') )



			if ($('#searchQuery').length != 0) {
					$('#searchQuery').watermark('Keyword(s)', {className: 'watermark'});


			}

				$('#postcode').watermark('Postcode / suburb', {className: 'watermark'});


			

				if ($('#shopGateway').length != 0) {

						$('#s_fname').watermark('Firstname', {className: 'watermark'});
						$('#s_lname').watermark('Surname', {className: 'watermark'});
					}

			if ($('#registerForm').length != 0) {

					$('#r_fname').watermark('Firstname', {className: 'watermark'});
					$('#r_lname').watermark('Surname', {className: 'watermark'});



					$('#copybilling').click(function() {
						if ($('#copybilling').attr('checked')) {
						$('input#s_address_1').val( $('input#b_address_1').val() )
						$('input#s_address_2').val( $('input#b_address_2').val() )
						$('input#s_postcode').val( $('input#b_postcode').val() )
						$('input#s_suburb').val( $('input#b_suburb').val() )
						$('input#s_state').val( $('input#b_state').val() )

						var scountry =  $('select#b_country option:selected').val();

						$('select#s_country option').each(function() {

							if ($(this).val() == scountry) {
								$(this).attr('selected', true)
							}

						})


						} else {

							 $('input#s_address_1').val('')
							$('input#s_address_2').val('')
							$('input#s_postcode').val('')
							$('input#s_suburb').val('')
								$('input#s_state').val('')
						}
					})
			}



			




			/* --- News (homepage) ---- */

				$('a#nextNews').click( function() {

				if ($(this).attr('class') != 'scrollNews disabRight') {
				$('#scrollerNews')	.animate({

					    left: '-=678'

					  }, 600, function() {
					    // Animation complete.
					  });


				$(this).addClass('disabRight')
				$('#prevNews').removeClass('disabLeft')
					}
				return false;

				})

				$('a#prevNews').click( function() {
					if ($(this).attr('class') != 'scrollNews disabLeft') {
				$('#scrollerNews')	.animate({

					    left: '+=678'

					  }, 600, function() {
					    // Animation complete.
					  });

						$(this).addClass('disabLeft')
						$('#nextNews').removeClass('disabRight')
							}
				return false;

				})



					$('a#nextConditions').click( function() {

					if ($(this).attr('class') != 'scrollNews disabRight') {
					$('#scrollerConditions')	.animate({

						    left: '-=678'

						  }, 600, function() {
						    // Animation complete.
						  });


					$(this).addClass('disabRight')
					$('#prevConditions').removeClass('disabLeft')
						}
					return false;

					})

					$('a#prevConditions').click( function() {
						if ($(this).attr('class') != 'scrollNews disabLeft') {
					$('#scrollerConditions')	.animate({

						    left: '+=678'

						  }, 600, function() {
						    // Animation complete.
						  });

							$(this).addClass('disabLeft')
							$('#nextConditions').removeClass('disabRight')
								}
					return false;

					})


				if ($('.sections').length != 0) {


				$('.sections a').click( function(){ 

					nameattr = $(this).attr('href')
					nameattr = nameattr.substr(1)
					//alert(nameattr)
					$.scrollTo( $('a[name="'+nameattr+'"]'), 600 )

					return false
					}) 
				
				$('p.backtotop a').click( function(){ 
					$.scrollTo( $('#main'), 600 )

					return false
					})
					
				}

			if ($('#highlights').length != 0) {

					t = setTimeout ( "slideshow()", 9000 );


				$('#highlights .right li a.cufon').click( function() {	

					$('#highlights .right div.details').css('display','none')

					$('#highlights .right li:last').removeClass('last')
					$('#highlights .right li:last').addClass('last')


					if ($(this).parent().attr('class') == 'last') {
						$(this).parent().removeClass('last')
					}
					var nextdiv = $(this).parent().next('div')
					$(nextdiv).css('display','block')

					return false;
				})





				$('#highlights .controls a').click( function() {

					if ($(this).attr('class') != 'pause') {


						if ($(this).attr('class') == 'next') {
							var activeImg = $('#highlights .activeImg')
							if ($(activeImg).next().length != 0) {
								$(activeImg).next().addClass('loadingImg') 
								} else {
									$('#highlights .image:first').addClass('loadingImg') 
								}

								var activeTxt = $('#highlights .activeTxt')
								if ($(activeTxt).next('.description').length != 0) {
									$(activeTxt).next('.description').addClass('loadingTxt') 
									} else {
										$('#highlights .description:first').addClass('loadingTxt') 
									}
											var activeEst = $('#highlights .activeEst')
											if ($(activeEst).next('.estimate').length != 0) {
												$(activeEst).next('.estimate').addClass('loadingEst') 
												} else {
													$('#highlights .estimate:first').addClass('loadingEst') 
												}


						} else {

								var activeImg = $('#highlights .activeImg')
								if ($(activeImg).prev().length != 0) {
									$(activeImg).prev().addClass('loadingImg') 
									} else {
										$('#highlights .image:last').addClass('loadingImg') 
									}

										var activeTxt = $('#highlights .activeTxt')
										if ($(activeTxt).prev('.description').length != 0) {
											$(activeTxt).prev('.description').addClass('loadingTxt') 
											} else {
												$('#highlights .description:last').addClass('loadingTxt') 
											}
													var activeEst = $('#highlights .activeEst')
													if ($(activeEst).prev('.estimate').length != 0) {
														$(activeEst).prev('.estimate').addClass('loadingEst') 
														} else {
															$('#highlights .estimate:last').addClass('loadingEst') 
														}

						}
						$(activeImg).fadeOut(500)	
						$(activeImg).removeClass('activeImg')
						$('#highlights .loadingImg').fadeIn(500)
						$('#highlights .loadingImg').addClass('activeImg')
						$('#highlights .activeImg').removeClass('loadingImg')	

							$(activeTxt).css('display','none')	
							$(activeTxt).removeClass('activeTxt')
							$('#highlights .loadingTxt').css('display','block')	
							$('#highlights .loadingTxt').addClass('activeTxt')
							$('#highlights .activeTxt').removeClass('loadingTxt')

								$(activeEst).css('display','none')	
								$(activeEst).removeClass('activeEst')
								$('#highlights .loadingEst').css('display','block')	
								$('#highlights .loadingEst').addClass('activeEst')
								$('#highlights .activeEst').removeClass('loadingEst')


					}

					clearInterval(t)
						t = setTimeout ( "slideshow()", 9000 );
					return false;
				})


			}
			/* ---- Watchlist ---- */

			$('a.remLot').click(function() {
				if (!confirm('Are you sure you want to remove this lot?')) {
					return false
				} else {

				var lotid = $(this).attr('id')
				lotid = lotid.substr(3)
				var sessid = $('input#sessionId').val()
				var ajaxurl = 'remlot?sess_id='+sessid+'&lot_id='+lotid;

				// Get session ID from hidden field
				$.ajax({ url: ajaxurl, context: document.body, success: function(data){
				   }});
				//alert(ajaxurl)
					$(this).parent().parent().parent('div.lotContainer').fadeOut(300);

				return false;

				}

			})


			/* ---- Bids summary ajax ---- */

				if ($('#bidsSummary').length != 0) {

					$('a.remBid').click(function() {
						if (!confirm('Are you sure you want to delete this bid?')) {
							return false
						} else {

						var bidid = $(this).attr('id')
						bidid = bidid.substr(3)
						var sessid = $('input#sessionId').val()
						var ajaxurl = 'rembid?sess_id='+sessid+'&bid_id='+bidid;
						//alert(ajaxurl)
					//	return false
					    var less = $(this).parent().parent().children('span.hiddenData').html()

						totalBids = recal(less)
						$('#totalBids').html(totalBids)

						// Get session ID from hidden field
						$.ajax({ url: ajaxurl, context: document.body, success: function(data){
						   }});
							$(this).parent().parent().parent('div.lotContainer').fadeOut(300);

						return false;

						}		

					})







						function addCommas(nStr)
						{
							nStr += '';
							x = nStr.split('.');
							x1 = x[0];
							x2 = x.length > 1 ? '.' + x[1] : '';
							var rgx = /(\d+)(\d{3})/;
							while (rgx.test(x1)) {
								x1 = x1.replace(rgx, '$1' + ',' + '$2');
							}
							return x1 + x2;
						}



					function recal(less)
					{
						var less = parseInt(less)
						var totalBids = 0

						$('span.hiddenData').each( function() {

							thisbid = $(this).html()
							thisbid = parseInt(thisbid)
							totalBids = (totalBids+thisbid)

						})

						totalBids = (totalBids-less)
						totalBids = addCommas(totalBids)
						return totalBids
					}

				}



			/* ---- Anti spam ---- */

			$(function(){


					$('span.mailme').each( function() {
					var at = / at /;
					var dot = / dot /g;
					var addr = $(this).text().replace(at,"@").replace(dot,".");
					$(this).after('<a class="grey" href="mailto:'+addr+'" title="Send an email">'+ addr +'</a>')
					.hover(function(){window.status="Send a letter";}, function(){window.status="";});
					$(this).remove();
					})
				});




	})


		function slideshow() {

			clearInterval(t)

			$('#highlights .controls a.next').click();

		}
