function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
		else this.q = null;
		this.keyValuePairs = new Array();
		
		if(q) {
			for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}

var ie6 = false;
var pageLoaded = false;
var goPageLoaded = false;

function getInternetExplorerVersion() {
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

if(getInternetExplorerVersion() == 6){
	ie6 = true;
}

$(document).ready( function(){
	
	// Homepage photo Swap
	$('div.banner div.right a').mouseover( function(){
		var bannerPhotos = $('div.banner div.left img');
		var bannerLinks = $('div.banner div.right a');
		var currentPos = $(bannerLinks).index(this);
		
		$(bannerPhotos).addClass('hide');
		$(bannerPhotos[currentPos]).removeClass('hide');
	});
	
	// Highlight current page
	var bodyTitle = $('body').attr('title');
	var allHeaderLinks = $('#header ul li a');
	for(var i=0; i < allHeaderLinks.length; i++){
		if($(allHeaderLinks[i]).html() == bodyTitle){
			$(allHeaderLinks[i]).parent().addClass('selected');	
		}
	}
	
	if(!ie6){
		resizeCheck();
	}
	
	var allTitles = $('table.glossary span.title');
	var allDefinitions = $('table.glossary span.definition');
	
	function enableBtns(){
		$('div.definition p a').click( function(){
			for(i = 0; i < allTitles.length; i++){
				if($(this).html().toLowerCase() == $(allTitles[i]).html()){
					$('div.definition h2').html($(allTitles[i]).html());
					$('div.definition p').html($(allDefinitions[i]).html());
					enableBtns();
				}
			}
			return false;
		});	
	}
	
	$(allTitles).click( function(){
		$('div.definition h2').html($(this).html());
		$('div.definition p').html($(this).siblings('span').html());
		enableBtns();
	});
	
	
	
		
	//---------- sliding window ----------//
	    var slidingElement;
	    var boxWidth;
	    var elementWidth;
	    var currentPosition;
		
		elementsLoaded = function(){
		    pageLoaded = true;
		    
		    // Element to be slid inside of overflow box
		    slidingElement = $('div.standard div.slides');
    		
		    // Width of the container (with css overflow applied)
		    boxWidth = $('div.standard').width();
			
		    // width of element inside of 
		    elementWidth = $(slidingElement).width() - boxWidth;

		    // get the position of the sliding element
		    currentPosition = $(slidingElement).css('left');
    		    
		    currentPosition = currentPosition.split('px');
		    currentPosition = currentPosition[0];
		}
		
		// on click of the previous button
		$('div.standard a.leftArrow').click( function(){
		    if(pageLoaded == false){
		        elementsLoaded();
		    }
			// create a positive number for comparison
			var absPositionValue = Math.abs(currentPosition);
			
			if(absPositionValue > 0){
				// subtract the width of the visible area from the current position
				currentPosition = (eval(currentPosition) + boxWidth);
				
				if(currentPosition == 0){
					// if slider is back to starting position disable previous button
					$('div.standard a.leftArrow').addClass('disabled');
				}
				
				var cssPosition = currentPosition+"px";
				
				$(slidingElement).each(function(){
					$(this).animate({left: cssPosition}, {duration: 500, easing: 'easeOutQuad'});
				});
				
				// if previous button clicked remove disabled class from the next button
				$('div.standard a.rightArrow').removeClass('disabled');
			}
			return false;
		});
		
		// on click of the next button
		$('div.standard a.rightArrow').click( function(){
		    if(pageLoaded == false){
		        elementsLoaded();
		    }
			// create a positive number for comparison
			var absPositionValue = Math.abs(currentPosition);
			
			// if the current position is less the the width of the sliding element
			if(absPositionValue <= (elementWidth-boxWidth)){
				// subtract the width of the visible area from the current position
				currentPosition = (currentPosition - boxWidth);
				
				if(Math.abs(currentPosition) >= (elementWidth-boxWidth)){
					// if slider is back to starting position disable previous button
					$('div.standard a.rightArrow').addClass('disabled');
				}
				
				var cssPosition = currentPosition+"px";
				
				$(slidingElement).each(function(){
					$(this).animate({left: cssPosition}, {duration: 500, easing: 'easeOutQuad'});
				});
				// if next button clicked remove disabled class from the previous button
				$('div.standard a.leftArrow').removeClass('disabled');
			}
			return false;
		});
		
		
		
	//---------- sliding window ----------//
	    var goSlidingElement;
	    var goBoxWidth;
	    var goElementWidth;
	    var goCurrentPosition;
		
		goElementsLoaded = function(){
		    goPageLoaded = true;
		    
		    // Element to be slid inside of overflow box
		    goSlidingElement = $('div.grandOpening div.slides');
    		
		    // Width of the container (with css overflow applied)
		    goBoxWidth = $('div.grandOpening').width();
			
		    // width of element inside of 
		    goElementWidth = $(goSlidingElement).width() - goBoxWidth;

		    // get the position of the sliding element
		    goCurrentPosition = $(goSlidingElement).css('left');
    		    
		    goCurrentPosition = goCurrentPosition.split('px');
		    goCurrentPosition = goCurrentPosition[0];
		}
		
		// on click of the previous button
		$('div.grandOpening a.leftArrow').click( function(){
		    if(goPageLoaded == false){
		        goElementsLoaded();
		    }
			// create a positive number for comparison
			var absPositionValue = Math.abs(goCurrentPosition);
			
			if(absPositionValue > 0){
				// subtract the width of the visible area from the current position
				goCurrentPosition = (eval(goCurrentPosition) + goBoxWidth);
				
				if(goCurrentPosition == 0){
					// if slider is back to starting position disable previous button
					$('div.grandOpening a.leftArrow').addClass('disabled');
				}
				
				var cssPosition = goCurrentPosition+"px";
				
				$(goSlidingElement).each(function(){
					$(this).animate({left: cssPosition}, {duration: 500, easing: 'easeOutQuad'});
				});
				
				// if previous button clicked remove disabled class from the next button
				$('div.grandOpening a.rightArrow').removeClass('disabled');
			}
			return false;
		});
		
		// on click of the next button
		$('div.grandOpening a.rightArrow').click( function(){
		    if(goPageLoaded == false){
		        goElementsLoaded();
		    }
			// create a positive number for comparison
			var absPositionValue = Math.abs(goCurrentPosition);
			
			// if the current position is less the the width of the sliding element
			if(absPositionValue <= (goElementWidth-goBoxWidth)){
				// subtract the width of the visible area from the current position
				goCurrentPosition = (goCurrentPosition - goBoxWidth);
				
				if(Math.abs(goCurrentPosition) >= (goElementWidth-goBoxWidth)){
					// if slider is back to starting position disable previous button
					$('div.grandOpening a.rightArrow').addClass('disabled');
				}
				
				var cssPosition = goCurrentPosition+"px";
				
				$(goSlidingElement).each(function(){
					$(this).animate({left: cssPosition}, {duration: 500, easing: 'easeOutQuad'});
				});
				// if next button clicked remove disabled class from the previous button
				$('div.grandOpening a.leftArrow').removeClass('disabled');
			}
			return false;
		});
	
	
	/*-- Browse By --*/
	var searchNav = $('div.searchBoxInner > ul').children('li');
	$(searchNav).click( function(){
		$('div.searchBox ul ul').hide();
		$(this).children('ul').show();
		$(searchNav).removeClass('selected');
		$(this).addClass('selected');
	});
	
	/*-- Browse By --*/
	var browseNav = $('div.wineNav > ul').children('li');
	$(browseNav).click( function(){
		var navHeight = $(this).children('ul').height();
		$(browseNav).removeClass('selected');
		$(this).addClass('selected');
		$(this).parent().height(navHeight+27);
		$('div.wineNav ul ul').hide();
		$(this).children('ul').show();
	});
	
	$('a.buyBottleBtn').click( function(){
		var pricePerBottle = $('div.pricePerBottle').html();
		var pricePerBottle_array = pricePerBottle.split("$");
		var url = "http://www.wineconnextion.com/bottleReserve.php?upc="+$('span.barcode').html()+"&quantity="+$('input.quantity').val() +"&productPrice="+ pricePerBottle_array[1] +"&productName="+ $('h2').html();
		$(location).attr('href',url);
		return false;
	});
	
	var isReservePage = $('body.bottleReserve');
	if(isReservePage.length > 0){
		var upc = queryString('upc');
		var quantity = queryString('quantity');
		var productPrice = queryString('productPrice');
		var productName = queryString('productName');
		$('div.bottleName').html(productName);
		$('div.quantity').html(quantity);
		var totalPrice = parseFloat(productPrice)*parseFloat(quantity);
		totalPrice = totalPrice.toFixed(2);
		$('div.totalPrice').html(totalPrice);
		
		$('input.hdnUPC').val(upc);
		$('input.hdnBottleName').val(productName);
		$('input.hdnQuantity').val(quantity);
		$('input.hdnTotalPrice').val(totalPrice);
	}
	if($('body#productwineinfoBody').length == 1){
		$('a.searchBtn').click( function(){
			var searchBoxValue = $('input.searchBox').attr('value');
			searchBoxValue = searchBoxValue.replace(/ /gi, "_");
			if(searchBoxValue != "Enter_search_keywords_here" && searchBoxValue != ''){
				window.location = '/index.php?main_page=advanced_search_result&search_in_description=1&keyword='+searchBoxValue;
			} else {
				alert('please enter a search term');
			}
			return false;
		});
		$('input.searchBox').keypress(function(event) {
			if (event.keyCode == '13') {
				var searchBoxValue = $('input.searchBox').attr('value');
				searchBoxValue = searchBoxValue.replace(/ /gi, "_");
				if(searchBoxValue != "Enter_search_keywords_here" && searchBoxValue != ''){
					window.location = '/index.php?main_page=advanced_search_result&search_in_description=1&keyword='+searchBoxValue;
				} else {
					alert('please enter a search term');
				}
				return false;
			} else {
			}
		});
	}
	
});

function resizeCheck() {
	var wrapperHeight = $('div#wrapper').height();
	var documentHeight = $(document).height();
	if(wrapperHeight < documentHeight){
		$('div#wrapper').height(documentHeight);	
	}
}

$(window).resize( function() {
	if(!ie6){
		resizeCheck();
	}
});