

Cufon.replace('#mnav a, #snav a, #footer p, .second h1, .second h2', {
	hover: true
});


var $j = jQuery.noConflict();
$j(document).ready(function() {
$j('html').addClass('hasjs');	
	
	//Fade in Images on page load
	$j('#imgviewer img, .col1 img').hide();
	$j('#imgviewer img, .col1 img').fadeIn(2000);
	
	//Safari fix for main nav
	var detect = navigator.userAgent.toLowerCase();
  if (checkIt('safari')){
  	//console.log('safari')
  	$j('.second #mnav').css('padding-top', '6px')
  }
  function checkIt(string){
  	var place = detect.indexOf(string) + 1;
  	return place;
  }

	
	//Gallery
	
	$j('#gnavcontainer').wrap('<div id="gnavwrap"></div>');
	var count = 0;
	var cWidth = 0;
	
	$j('#gallerynav li img').each(function() {
		//console.log(cWidth); //debug check
		
		if (count > 0){
			$j(this).css('opacity','0.5');
			cWidth= cWidth + $j(this).width() + 11;
			count++;
		} else {
			cWidth= cWidth + $j(this).width() + 11;
			count++;
		}
		
	$j('#gallerynav li:nth-child(7n)').addClass('seventh');
	
	});
	
	l = ($j('#gallerynav li').length) * 117;
	$j('#gnavcontainer').width(l);
	
	$j('#gnavwrap').after('<img id="buttonLeft" src="'+base+'img/basic/btn-l.gif" alt="" /><img id="buttonRight" src="'+base+'img/basic/btn-r.gif" alt="" />');
	$j('#buttonLeft').css({
		'opacity': '0.5',
		'cursor' : 'default'
		});
	
	$j('#buttonLeft').click(function(){
		cWidth = ($j('#gnavcontainer').width());
		if ( $j(this).css('opacity') != '0.5' ){
			if ($j('#gnavcontainer').is(':animated')) {
				//console.log('no');
			} else {
				var sliderPos = $j('#gnavcontainer').css('left').replace('px', '');
				
				if ( (sliderPos < 0) && (sliderPos >= -cWidth ) ) {
					$j('#gnavcontainer').animate({left: '+=805'}, 1000, function(){
						var newSliderPos = $j('#gnavcontainer').css('left').replace('px', '')
						//console.log(newSliderPos);
						if(newSliderPos == 0) {
							$j('#buttonLeft').css({'opacity': '0.5','cursor' : 'default'});
						}
					});
					$j('#buttonRight').css({'opacity': '1','cursor' : 'pointer'});	
				};
			}; 
		};
		return false;
	});
	
	
	$j('#buttonRight').click(function(){
		cWidth = ($j('#gnavcontainer').width());
		if ( $j(this).css('opacity') != '0.5' ){
			if ($j('#gnavcontainer').is(':animated')) {
				//console.log('no');
			} else {
				var sliderPos = $j('#gnavcontainer').css('left').replace('px', '');
				
				if ( (sliderPos <= 0) && (sliderPos > -(cWidth - 805)) ) {
					$j('#gnavcontainer').animate({left: '-=805'}, 1000, function(){
						//console.log(-(cWidth-805));
						var newSliderPos = $j('#gnavcontainer').css('left').replace('px', '')
						if(newSliderPos < -(cWidth - 806)) {
							$j('#buttonRight').css({'opacity': '0.5','cursor' : 'default'});
						}
					});
					$j('#buttonLeft').css({'opacity': '1','cursor' : 'pointer'});	
				};
			};
		};
		return false;
	});
	
	//function: click on thumbnail, get image
	$j('#gallerynav li a').live("click", function(){
		if ($j(this).hasClass('selected')) {} else{
			var imgsrc= $j(this).attr('href');
			var c ='';
			if($j(this).parent('li').hasClass('seventh')) {
				var c = 'seventh';
			};
			//console.log(imgsrc);
			$j('.selected img').animate({opacity: '0.5'}, 250).parents('.selected').removeClass('selected');
			$j(this).animate({opacity: '1'}, 250).addClass('selected');
			$j('<img class="loading" />').attr('src', imgsrc).load(function(){
				$j('#imgviewer').append($j(this));
				$j('#imgviewer .loading').fadeIn(500, function(){
					$j('#imgviewer img').addClass(c).prev().remove();
				}).removeClass('loading');
			});
		};
		return false;
	});
	
	//function for clicking on image
	$j('#imgviewer img').live("click", function(){
			var thissrc= $j(this).attr('src');
			if ($j(this).hasClass('seventh')){
				//console.log('seventh');
				cWidth = ($j('#gnavcontainer').width());
				if ( $j(this).css('opacity') != '0.5' ){
					if ($j('#gnavcontainer').is(':animated')) {
						//console.log('no');
					} else {
						var sliderPos = $j('#gnavcontainer').css('left').replace('px', '');
						
						if ( (sliderPos <= 0) && (sliderPos > -(cWidth - 805)) ) {
							$j('#gnavcontainer').animate({left: '-=805'}, 1000, function(){
								//console.log(-(cWidth-805));
								var newSliderPos = $j('#gnavcontainer').css('left').replace('px', '')
								if(newSliderPos < -(cWidth - 806)) {
									$j('#buttonRight').css({'opacity': '0.5','cursor' : 'default'});
								}
							});
							$j('#buttonLeft').css({'opacity': '1','cursor' : 'pointer'});	
						};
					};
				};
			}
			
			//loop through all thumbnails and get match to src
			$j('#gallerynav li a').each(function(){
				if($j(this).attr('href') == thissrc ) {
					var c =''
					if($j(this).parent('li').next('li').hasClass('seventh')){
						var c ='seventh';
					};
					var next = $j(this).parent('li').next().children('a');
					var nextsrc = next.attr('href');
					if ( next.length > 0 ) {
						$j('.selected img').animate({opacity: '0.5'}, 250).parents('.selected').removeClass('selected');
						$j(this).parent('li').next().children('a').addClass('selected').children('img').animate({opacity: '1'}, 250);
						$j('<img class="loading" />').addClass(c).attr('src', nextsrc).load(function(){
							$j('#imgviewer').append($j(this));
							$j('#imgviewer .loading').fadeIn(500, function(){
								$j('#imgviewer img').prev().remove();
							}).removeClass('loading');
						});
					};
				};
			});
		return false;
	});
	
	
	
	$j('#gallerynav li img').hover(
		function () {  
      $j(this).animate({opacity: '1'}, 250);
    }, 
    function () {
    	if ($j(this).parents().hasClass('selected')) { } else{
      	$j(this).animate({opacity: '0.5'}, 250);
      	}
      }
  );
  
  /****blog****/
  
  if ($j('body').is('.blog')) {
  //console.log('blog');
	  $j('#post-subscribe li').hide(0);
	  
	  $j('#post-subscribe #open').click(function(){
	  	if ($j('#post-subscribe li').is(':visible')){
	  		$j('#post-subscribe li').hide(500);
	  	} else {
	  		$j('#post-subscribe li').show(500);
	  	}
	  	return false;
	  });
	  
	  $j('#post-subscribe li').click(function(){
			return true;
		});
		
		$j('.archive .open').removeClass('open').addClass('closed');
		
		$j('.archive li').click(function(){
			if( $j(this).is('.open') ) {
				$j(this).removeClass('open').addClass('closed');
			} else {
				$j(this).removeClass('closed').addClass('open');
			}
		})
	}
	
	
	var swap_text_boxes = [];

	function init_swap_text_boxes(){
	  //Store the default value for each box
	  var target = $j('input[type=text][value].swaptextbox,textarea.swaptextbox');
	  target.each(function() {
	    swap_text_boxes[$j(this).attr('id')] = $j(this).attr('value');
	  });
	  //Add focus and blur events to set or clear the value
	  target.bind('focus', function() {
	    if($j(this).val() == swap_text_boxes[$j(this).attr('id')]) {
	      $j(this).val('');
	    }
	  });
	  target.bind('blur', function() {
	    if($j(this).val() == '') {
	      $j(this).val(swap_text_boxes[$j(this).attr('id')]);
	    }
	  });
	}

	init_swap_text_boxes();
	
});


