$(document).ready(function() {


	


		function is_int(value){
		  if((parseFloat(value) == parseInt(value)) && !isNaN(parseInt(value))){
		      return true;
		 } else {
		      return false;
		 }
		}	
		
		
		
		
		/*  --- 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; 
		});
		
		
		/* ---- Navigation drop down (global) ---- */
		
		/*$('#nav .drop').hover( function () {
		
			$('#auctionDrop').css('display','block')
			
		}, function() {
			
				$('#auctionDrop').css('display','none')
			
		})*/

			if ($('#article').length != 0) {
				($('.body p:first').addClass("intro"))
			}
			
				$('#breakout').insertAfter($('.body p:eq(2)') )
			
		
		
		if ($('#kwSearch').length != 0) {
				$('#kwSearchInput').watermark('Keyword(s)', {className: 'watermark'});
		
	
		}
		
		
		
		
			$('#e_email').watermark('Your email', {className: 'watermark'});
		
		$('.printThis').click(function() {
			window.print()
			return false;
		})
		
	
			
			$('a.expand').click( function() {
				var nextlist = $(this).parent().children('ul')
				if ($(this).attr('class') == 'expand contract') {
					nextlist.hide()
					$(this).removeClass('contract')
				} else {
					nextlist.show()
					$(this).addClass('contract')
				}
			
				return false;
			})
			
			
			

		
		if ($('#priceListing').length != 0) {
			
			$('div.result').click( function(e){
			
			  if($(e.target).is('div')){
			//a//lert(   $(this).attr('class') )
			//	alert(   $(this).children('div.lot').children('a').attr('href') )
			window.location = $(this).children('div.lot').children('a').attr('href') ;
			//	$(this).children('div.lot').children('a').click()
			  }
			});
			
			
		}
		if ($('form.payments').length!=0) {
			$('#p_fname').watermark('Firstname', {className: 'watermark'});
			$('#p_lname').watermark('Surname', {className: 'watermark'});
		}
		
			if ($('form#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('')
					}
				})
		}
		
		
		
		/* ---- Refiners on lot listing ---- */
			if ($('#catWidgets').length != 0) {

				$('ul.category').each( function() {
					
					$(this).children('li:last').addClass('last')
					
				})
		
		$('ul.selectWrap li').hover( function() {
				$(this).css('cursor', 'pointer')
				$(this).css('color', '#870909')
				if (($(this).children('a').attr('class') == 'selector') || ($(this).children('a').attr('class') == 'selector short')) {
				$(this).children('a').css('background-position', 'right -56px')
				$(this).children('a').children('span').css('background-position', 'left -84px')
			}
				$(this).children('ul').show()
				
				if ($(this).attr('class') == 'session') {
				
					var childY = ($(this).children('ul').height())
					var thisY = $(this).position()
					var thisY = thisY.top;
					var sessionY = ($(this).parent('ul').height())
					
					if ((childY + thisY) > (sessionY) ){
						//	alert(childY+' '+thisY+' '+sessionY)
						if (childY > sessionY) {
						//child is taller than parent, align at top
						($(this).children('ul').css('top', '-'+thisY+'px'))
						} else {
							//child is shorter than parent, align at base	
					//	alert('shorter')
							var newTop =((thisY+childY) - sessionY) 
						//	alert(newTop)
								$(this).children('ul').css('top', '-'+newTop+'px')		
						}
					}
				}
		}, function() {
			$(this).css('cursor', 'hover')
			$(this).css('color', '#333333')
			if (($(this).children('a').attr('class') == 'selector') || ($(this).children('a').attr('class') == 'selector short')) {
			$(this).children('a').css('background-position', 'right 0')
			$(this).children('a').children('span').css('background-position', 'left -28px')
			}
			$(this).children('ul').hide()	
		})
		var sessionY = $('ul.session').height()	
		
		

		
	}
		
		
		
		/* ---- View lot ---- */
		
			if ($('#currency').length != 0) {
				
				
			
				
				
			$('#currency').click( function() {
				
				if ($('#currencyConverter').css('display') == 'block') {	
				
					$('#currencyConverter').hide()
				} else {
				$('#currencyConverter').show()
				}
				
					return false
			})
			
			$('#closeBox').click( function() {
					$('#currencyConverter').hide()
					return false
			})
			
			}
		 
		
		/* --- Auction highlights (homepage) ---- */
		
		if ($('#highlights').length != 0) {
	
		if ($('#hcount').val() > 1) {
				t = setTimeout ( "slideshow()", 9000 );
			} else {
				$('div.controls').css('display','none')
			}
			
			$('#highlights .right li a.cufon').click( function() {	
			
				$('#highlights .right li.details').css('display','none')
				
				$('#highlights .right li:eq(8)').removeClass('last')
				$('#highlights .right li:eq(8)').addClass('last')
				
				
				if ($(this).parent().attr('class') == 'tab') {
					$(this).parent().removeClass('last')
				}
				
				if ($(this).parent().attr('id') == 'finalTab') {
					$(this).parent().removeClass('last')
				}
				
				var nextli = $(this).parent().next('li')
				$(nextli).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('div').length != 0) {
								$(activeImg).prev('div').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;*/
			
			}

		})


		$('#confirmSubmit').submit( function() {
			
			$('#confirmSubmitBtn').attr('disabled', true)
			$('#confirmSubmitBtn').attr('value','Processing, please wait....')

			
		})
		
		
		/* ---- 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;
					
					}		
					
				})
				
				
				
				
				
				
				$('#collectEnable').click(function() {
					$('#totalShipping').html('0.00')
					$('#totalPrice').html( $('#zeroShipping').val() )
				//	alert('collect')
					$('#collectionAddress').attr('disabled',false)
					
					
				})
				
					$('#shippingAddress').click(function() {

						$('#collectionAddress').attr('disabled',true)
						
							$('#totalShipping').html( $('#retainedShipping').val() )
							$('#totalPrice').html( $('#retainedTotal').val() )

					})
			
					$('#increaseBids').click(function() {
						if ($(this).attr('checked')) {
						$('#increaseAmount').attr('disabled',false)
						} else {
								$('#increaseAmount').attr('disabled',true)	
						}
					})
			
					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();
	
	}


