function mainmenu(){
	
	// Categories Menu Bar
	$('#mainbar ul').css('display', 'none').css('border-top','2px solid #FFF').addClass('rounded_bottom mainbar_shadowed');
	$('#mainbar li:has(ul) > a').append('&nbsp; &rsaquo;');
	$('#mainbar ul ul').css('border','0').addClass('rounded_right mainbar_shadowed');
	$('#mainbar li').hover(function(){
	
			$(this).find('ul:first').css({visibility: 'visible', display: 'none'}).slideDown(400);
	
		},function(){
			
			$(this).find('ul:first').css({visibility: 'hidden'});
					
		});
		
	$('#mainbar > li').hover(function(){
	
			$('a:first', this).addClass('hovered');
				
		},function(){
			
			$('a:first', this).removeClass('hovered');
					
		});
	
	$('#mainbar > li ul li a').css('width' , '162px');
	$('#mainbar a').removeAttr('title');
	$("#mainbar ul ul ul").remove();

}

$(document).ready(function(){

	// Start Main Menu
	mainmenu();

	// Search Binds - Do not modify
	$('#content_search input').keydown(function(e) {
		if (e.keyCode == 13) {
			contentSearch();
		}
	});
	
	$('#search_bar input').keydown(function(e) {
		if (e.keyCode == 13) {
			moduleSearch();
		}
	});
	
	// Language & Currency Tooltip Starter
	$("#selector_currency, #selector_language").tooltip({
	
	   // tweak the position
	   offset: [2, 2],
	   
	   fadeInSpeed: 300,
	   fadeOutSpeed: 600,
	
	   // use the "slide" effect
	   effect: 'fade'
	
	// add dynamic plugin with optional configuration for bottom edge
	}).dynamic({ bottom: { direction: 'down', bounce: true } });
	
	// Main menu active url change styles
	route = getURLVar('route');
	if (!route) {
		$('#tab_home').addClass('account_bar_active');
	} else {
		part = route.split('/');
		
		if (route == 'common/home') {
			$('#tab_home').addClass('account_bar_active');
		} else if (route == 'account/login') {
			$('#tab_login').addClass('account_bar_active');
		} else if (part[0] == 'account') {
			$('#tab_account').addClass('account_bar_active');
		} else if (route == 'checkout/cart') {
			$('#tab_cart').addClass('account_bar_active');
		} else if (part[0] == 'checkout') {
			$('#tab_checkout').addClass('account_bar_active');
		} 
	}
	
});

// Header Search Animation
function search_ani(text,clear){

	if (clear == 'true') { $('#keyword').val(''); }
	
	$('#filter_category_id').css('width' , 'auto').fadeIn('slow');
	$('#go_button').html(text);
	
}
// Header Search Animation END

// Product Page Quantity
function modQuantity(modifier){
	$(this).click(clearSelection);
	current_value = parseInt($('#total_to_add').val());
	if (modifier == 'plus'){	
		$('#total_to_add').val(current_value+1);
		$('#total_to_show').text(current_value+1);
	} 
	if (modifier == 'minus' && current_value > 1){
		$('#total_to_add').val(current_value-1);
		$('#total_to_show').text(current_value-1);
	}	
}
// Product Page Quantity END

// Shows terms and conditions
function termsloader(){
	$('#termsloader').slideToggle();
	$('html, body').animate({scrollTop: $("#anchorpal").offset().top}, 'slow');
}
// Shows terms and conditions END

// Scrolls smoothly to the top of the page
function scroll_up(){
	$('html, body').animate({scrollTop:0}, 'slow');
}
// Scrolls smoothly to the top of the page END

// Prevent Text Selection on Double Click
function clearSelection() {
  var sel ;
  if(document.selection && document.selection.empty){
    document.selection.empty() ;
  } else if(window.getSelection) {
    sel=window.getSelection();
    if(sel && sel.removeAllRanges)
      sel.removeAllRanges() ;
  }
}
// Prevent Text Selection on Double Click END


//// Default JS Shipped with OpenCart Default Template

function moduleSearch() {

	url = 'index.php?route=product/search';
	
	var keyword = $('#keyword').attr('value');
	
	if (keyword) {
		url += '&keyword=' + encodeURIComponent(keyword);
	}
	
	var category_id = $('#category_id').attr('value');
	
	if (category_id) {
		url += '&category_id=' + encodeURIComponent(category_id);
	}
	
	location = url;

}

function contentSearch() {

	url = 'index.php?route=product/search';
	
	var keyword = $('#keywordsearch').attr('value');
	
	if (keyword) {
		// first
		url += '&keyword=' + encodeURIComponent(keyword);
	}
	
	var category_id = $('#category_id').attr('value');
	
	if (category_id) {
		url += '&category_id=' + encodeURIComponent(category_id);
	}
	
	if ($('#description').attr('checked')) {
		url += '&description=1';
	}
	
	if ($('#model').attr('checked')) {
		url += '&model=1';
	}
	
	location = url;
	
}

function bookmark(url, title) {

	if (window.sidebar) { // firefox
    window.sidebar.addPanel(title, url, "");
	} else if(window.opera && window.print) { // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if(document.all) {// ie
   		window.external.AddFavorite(url, title);
	}
	
}

function getURLVar(urlVarName) {
	var urlHalves = String(document.location).toLowerCase().split('?');
	var urlVarValue = '';
	
	if (urlHalves[1]) {
		var urlVars = urlHalves[1].split('&');

		for (var i = 0; i <= (urlVars.length); i++) {
			if (urlVars[i]) {
				var urlVarPair = urlVars[i].split('=');
				
				if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	
	return urlVarValue;
}
