
var $j = jQuery

$j(document).ready(function(){

	/*******
	** Tablesortercall - 0.0.1 **
	*******/

	/*******
	** vp - first table link click - (c) - 0.0.1 **
	*******/
	if ( $j('table tbody th').length > 0 ) {
		$j('table tbody th').click(function(event){
			var el = $j(this).find(":nth-child(1)");
			if (el.attr('href') ) {
				document.location = el.attr('href')
			}
			
		});
	
	}
	
	/*******
	** vp - moreOptionsToggler (MGGG form) - (c) - 0.0.2 **
	*******/
	if ( $j('.moreOptionsToggler').length > 0 ) {
		// do not hide the options if a checkbox is selected.
		if ( !$j('#moreOptions').hasClass('selected') && !$j('#moreOptions ul li input[type=checkbox]').is(':checked')){
			$j('#moreOptions').hide();
		}
		else {
			$j('.moreOptionsToggler').addClass('less');
		}
		$j('.moreOptionsToggler').click(function(event){
			
			if ( $j(this).hasClass('less') ) {
				$j('#moreOptions').animate({
					height: 'toggle'
				}, 500, function() {
					// Animation complete.
				});
				$j(this).removeClass('less')
			}
			else {
				$j('#moreOptions').animate({
					height: 'toggle'
				}, 500, function() {
					// Animation complete.
				});
				$j(this).addClass('less')
			}
			event.preventDefault();
		});
		
		$j('.moreOptionsToggler').keyup(function (event) {
			if (event.keyCode == 79) {
				//open
				if (! $j(this).hasClass('less') ) {
					$j('#moreOptions').animate({
						height: 'toggle'
					}, 500, function() {
						// Animation complete.
					});
					$j(this).addClass('less')
				}
			} 
			else if (event.keyCode == 83) {
				if ( $j(this).hasClass('less') ) {
					// close
					$j('#moreOptions').animate({
						height: 'toggle'
					}, 500, function() {
						// Animation complete.
					});
					$j(this).removeClass('less')
				}
			}
		});
		
	}

	/*******
	** vp - Accordion - (c) - 0.0.2 **
	*******/
	if ( $j('.accordion .item').length > 0 ) {
		$j('.accordion .item h2.accordionHeader').each(function() {
			$j(this).find('a').click(function(event){
				if ( $j(this).hasClass('selected') ) {
					$j(this).removeClass('selected');
					$j(this).parent().next().animate({
						height: 'toggle'
					}, 500, function() {
						// Animation complete.
				  	});
				}
				else {
					// close all
					$j(this).parent().parent().parent().children().each(function(){
						if ( $j(this).find('h2.accordionHeader a').hasClass('selected')) {
							$j(this).find('h2.accordionHeader a').removeClass('selected');
							$j(this).find('.edgedContainer').animate({
								height: 'toggle'
							}, 500, function() {
								// Animation complete.
								$j(this).parent().removeClass('selected');
							});
						}
					});
					
					$j(this).addClass('selected');
					$j(this).parent().next().animate({
						height: 'toggle'
					}, 500, function() {
						// Animation complete.
				  	});
				}
				event.preventDefault();
    		});
		});
	}
	
	/*******
	** vp - Facts and Figures - (c) - 0.0.2 **
	*******/
	if ( $j('.factsAndFigures .item').length > 0 ) {
		$j('.factsAndFigures .item').each(function(i) {
			if ( $j(this).attr('class').indexOf('pos') != -1 ) { 
				var orgClass = $j(this).attr('class');
				var firstPos = orgClass.slice(orgClass.indexOf('pos'));
				if (firstPos.indexOf('selected') != -1 ) var cleanPosClass = firstPos.substr(0, firstPos.indexOf('selected') -1);
				else var cleanPosClass = firstPos
				var posClass = cleanPosClass.substr(3, cleanPosClass.length);
				var posLeft = posClass.substr(0, posClass.indexOf('x'));
				var posTop = posClass.substr(posClass.indexOf('x') + 1, posClass.length);
				$j(this).css({left: posLeft + 'px', top: posTop + 'px' })
			}
			$j(this).find('a').click(function(event){
				if ( $j(this).parent().hasClass('selected') ) {
					$j(this).parent().find('.description').animate({
						height: 'toggle'
					}, 250, function() {
						// Animation complete.
						$j(this).parent().removeClass('selected');
					});
				}
				else {
					// close all
					$j(this).parent().parent().children().each(function(){
						if ( $j(this).hasClass('selected')) {
							$j(this).find('.description').animate({
								height: 'toggle'
							}, 250, function() {
								// Animation complete.
								$j(this).parent().removeClass('selected');
							});
						}
					});
					
					$j(this).parent().addClass('selected')
					//$j(this).parent().find('.description').fadeIn();
					$j(this).parent().find('.description').animate({
						height: 'toggle'
					}, 250, function() {
						// Animation complete.
					});
				}
				event.preventDefault();
			});
		});
	};

    // if the rates slider exist on the page set the timeouts so it rotates.
    if($j('.rates ul li').length > 0){
		$j('.rates ul li').each(function(){
			$j(this).addClass('notvisible');
		});
        setTimeout('rateSlider()', 7000);
    }


	/*******
	** vp - search box text swapper - (c) - 0.0.1 **
	*******/
	$j('#querytext').focus(function() {
		var title = $j(this).attr('title');
		if ( $j(this).val() == title ) $j(this).val('')
	});
	$j('#querytext').blur(function() {
		var title = $j(this).attr('title');
		if ( $j(this).val() == '' ) $j(this).val(title)
	});


	/*******
	** vp - Second level navigation - (c) - 0.0.1 **
	*******/
    $j(".secondLevel ul li:has(ul)").addClass('hashChilds');
	$j(".secondLevel ul.nav li:nth-child(7)").addClass('seven');

	$j(".secondLevel ul li.hashChilds").hover(function(){
    
        $j(this).addClass("hover");
        $j('ul:first',this).css('display', 'block');
    
    }, function(){
    
        $j(this).removeClass("hover");
        $j('ul:first',this).css('display', 'none');
    
    });
	

	/*******
	** vp - hoverImage - (c) - 0.0.1 **
	*******/
	$j('.hoverImage').hover(function (e) {
		this.src = this.src.replace("-normal","-hover");
	 },
	function() {
		this.src = this.src.replace("-hover","-normal");
	});


	/*******
	** vp - Brandbox - (c) - 0.0.1 **
	*******/
	if ( $j('.brandBox').length > 0 && jQuery().cycle ) {
		$j('#navBrandBox').empty();
		$j('.brandBoxWrapper')
		.cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout: 10000,
			speed: 1000,
			fastOnEvent: 300,
			pager: $j('#navBrandBox'),
			pagerAnchorBuilder: function(index, DOMelement) {
				if (index == 0) {
					return '<li class="first-child"><a href="?boxtab=' + index +'">' + $j(DOMelement).find(".linkText").eq(0).text() + '</a></li>';
				}
				else if (index == 5) {
					return '<li class="last-child"><a href="?boxtab=' + index +'">' + $j(DOMelement).find(".linkText").eq(0).text() + '</a></li>';
				}
				else {
					return '<li><a href="?boxtab=' + index +'">' + $j(DOMelement).find(".linkText").eq(0).text() + '</a></li>';
				}
			}
		});
		
		$j('.pause').click(function(event) {
			$j('.brandBoxWrapper').cycle('pause');
			event.preventDefault();
		});

		$j('.resume').click(function(event) {
			$j('.brandBoxWrapper').cycle('resume');
			event.preventDefault();
		});
		
		/*
		if ( $j(this).parent().hasClass('selected') == false) {
			$j('.brandBoxNav .selected').removeClass('selected').children(0).animate({top: 0}, 'fast', function() {});
			
			$j(this).css({'top' : -15});
			$j(this).parent().addClass('selected');
			
			var ids = getUrlVars( $j(this).attr('href') )["brandboxitem"];
			$j('#' + ids).css({'display' : 'block'}).animate({
				left: 0
			}, 500, function() {
				
			});
			//$j('.brandBoxWrapper .selected').removeClass('selected').animate({
			//		left: '950px',
			//		display: 'none'
			//	}, 250, function() {
				
			//});
			$j('#' + ids).addClass('selected');
		}
		
		*/
	}

});


function rateSlider(){
	if($j('.rates ul li.selected').next('li').length == 0){
		$j('.rates ul li.selected').removeClass('selected').siblings(':first').addClass('selected');
	}
	else {
		$j('.rates ul li.selected').removeClass('selected').next('li').addClass('selected');
	}
	setTimeout('rateSlider()', 7000);
}

