/*
 * author:binu@dronasys.com
 * March 2009
 */

var sliderLeft = 0;
var slideTimer = '';
var slideDir = 1;
var imageHldWidth = 0;
var DSimageNum = 0;

$(document).ready(function(){

	$curSubMenu = $('#sideMenu ul li.current_page_item a').html();

	$('#mainImage #imageInfo').css('opacity',0.7);
	  $('#thumbCnt').mousemove(function(e){
    
		var pageLeft = $('#page')[0].offsetLeft; 
		var cntLeft = $('#thumbCnt')[0].offsetLeft; 
		//var midPoint = pageLeft + cntLeft + 350;
		var midPoint =  cntLeft + 350;
		slideDir =  parseInt((midPoint - e.pageX)/60);

		//$('#controls p').html(e.pageX +', '+ e.pageY + ', '+ pageLeft + ', ' + cntLeft + ', ' + slideDir + ', ' + slideTimer);
		
	  }); 
	  
	  $('#thumbCnt').bind("mouseenter",function(e){
	  	if (slideTimer === '') {
			//clearTimeout(slideTimer);
			slideTimer = setInterval("slideThumbs()", 20);
		}
	  });
	  
	  $('#thumbCnt').bind("mouseleave",function(e){
	  	clearTimeout(slideTimer);
		slideTimer = '';
	  });

/* lr controls */
	  $('#mainImage').bind("mouseenter",function(e){
	  	$('#imageMainCntrls').fadeIn();
	  });
	  
	  $('#mainImage').bind("mouseleave",function(e){
	  	$('#imageMainCntrls').fadeOut();
	  });

	  $('#imageMainCntrls a').bind("mouseenter",function(e){
	  	$(this).stop().animate({opacity: 0.8},600);
	  });
	  
	  $('#imageMainCntrls a').bind("mouseleave",function(e){
	  	$(this).stop().animate({opacity: 0.2},600);
	  });

	$('#imageMainCntrls a.left').click(function(){
		stepNextImage(-1);
	});

	$('#imageMainCntrls a.right').click(function(){
		stepNextImage(1);
	});

	
	initPortfolio();
	switch($curSubMenu) {
		case null:

				break;
		case 'viewPrints':
				$('a.order').attr('href','/prints/printinfo/?order=0');
				break;
		case 'printInfo':
				initForm();
				break;			
	}
	

});

function stepNextImage(dir) {
	var n = $('#imageHld ul li').length -1;
	var imageNum = DSimageNum + dir;
	if (imageNum <0 ) {imageNum = n;}
	if (imageNum > n ) {imageNum = 0;}
	updateMainImage($('#imageHld ul li:eq(' + imageNum + ') a'),imageNum);
}

function initForm() {
	var title = $('#orderForm div.img img').attr('alt');
	var thumb = $('#orderForm div.img img').attr('src');	
	$('#cf2_field_1').val(title);	
	$('#cf2_field_2').val(thumb);	
}

function initPortfolio() {
	var n = $('#imageHld ul li').length;
	if (n <= 0)  return;
	for (var i = 0; i < n; i++) {
		$('#imageHld ul li:eq(' + i + ') a').unbind('click').bind('click', {'colNum': i}, function(event){
	        var colNum = event.data['colNum'];
			$('a.order').attr('href','/prints/printinfo/?order='+colNum);
			updateMainImage(this,colNum);
			return false;
		});
	}	
		/*
		$('#controls a').toggle(function() {
			$('#mainImage div').animate({height:'200px',top:'266px'});
			return false;
			
		}, function() {
			$('#mainImage div').animate({height:'0px',top:'466px'});
			return false;			
		});
		*/
		updateMainImage('#imageHld ul li a:eq(0)',0);
		$('#imageHld ul li:last').addClass('last');
		imageHldWidth = ($('#imageHld ul li').length) * 75 -5;
		$('#imageHld').css('width',imageHldWidth + 'px');

}

function slideThumbs() {

	var direction = slideDir ;
	var prevSliderLeft = sliderLeft;
	sliderLeft = sliderLeft + direction;
	if (sliderLeft > 0) sliderLeft = 0; 	
	if ((sliderLeft + imageHldWidth) < 600 ) sliderLeft = prevSliderLeft;
	if (direction > 0 ) {
		//
	} else {
		//if (sliderLeft > 1000) sliderLeft = 2000; 	
	}
	//$('#imageHld').stop().animate({left: sliderLeft + 'px'},400);
	$('#imageHld').css('left',sliderLeft +'px');	
	//$('#controls p').html(sliderLeft + ' ' + imageHldWidth);
	
}


function updateMainImage(mythis,num) {

	var imgName = $(mythis).children('img').attr('src');
	var vertical=new RegExp(/311/);
	//var newImage = imgName.replace(/70x45.jpg/, "699x466.jpg");
	//var newImage = imgName.replace(/70x45.jpg/, "310x465.jpg");
	//var newImage = imgName.substr(1,l) +  "699x466.jpg");
	var newImage = $(mythis).next('span').next('span').html();
	var width = $(mythis).next('span').next('span').next('span').html();
	if (vertical.test(width)) {
		$('#imageInfo').addClass('vertical');
	} else {
		$('#imageInfo').removeClass('vertical');
	}
	//$('#picHld div').html($(mythis).attr('title'));
	loadImage(newImage,'#mainImage',num);
	$('#imageInfo div').html($(mythis).next('span').html());
	$('#controls p').html($(mythis).next('span').children('h3').html());			
}


function loadImage(url,holderId,num) {

	//var img = new Image();
  	var img = holderId + ' img#mainImageImg';

	  // wrap our new image in jQuery, then:
  $(img)
    // once the image has loaded, execute this code
    .load(function () {
      // set the image hidden by default    
      
    	$(this).hide();
	
      // with the holding div #loader, apply:
    //  $('#picHld img').replaceWith(this);
        
    
      if ($(holderId).hasClass('loading')) {
	  		$(holderId).removeClass('loading');
	  		$(holderId).addClass('thumbPic');
	  	}
      $(this).fadeIn(function(){
		$('#imageInfo').show();	
		var newW = $('#mainImage img')[0].offsetLeft; 
		
		$('#controls p').css('padding-left',newW);

	  });
    })
    
    // if there was an error loading the image, react accordingly
    .error(function () {
      // notify the user that the image could not be loaded
    })
    		$('#imageInfo').hide();
    // *finally*, set the src attribute of the new image to our image
    $(img).fadeOut(200,function(){
		//$(holderId).removeClass('thumbPic');

		$(holderId).addClass('loading');
		
		$(img).attr('src', url);	
		DSimageNum= num;

	});
	
	 
}
