/**
 * @author Kris
 */


function balePop(path){
var day = new Date();
var id = day.getTime();

newpage = window.open(path,  id , 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=680,height=500,screenX=20,screenY=20');

}


 /**
 * the nav block controller for moving in and out
 
baleNavBlock = {
	_divId: null,
	init: function(divId){
		_divId = divId;
		$(_divId).hover(this.moveIn, this.moveOut);
		if ($('body').attr('id') == 'bale-home') {
			this.teaseNav();
			
		}
	//lert($(_divId));
		//$(divId).hover(function(){alert('over')} , this.moveOut);
	},
	
	teaseNav:function(){
		$(_divId).animate({
				left: 0
			}, 800, 'linear', function(){
				$(_divId).animate({
					left: 0
				}, 200, 'linear', function(){
					//$(_divId).animate({left: -150}, 800);
					})
			})	
	},
	moveOut : function(){	
     	$(_divId).stop().animate({
      	left:-150
      }, 750 );
	},
	moveIn : function(){
		//alert('init nav');
		//console.log("MoveIn() Nav Block", "info");
  	  $(_divId).stop().animate({
      	left:0
      }, 750 );
	}
}
*/

function balePhotoTransition(div){
	var _currentPhotoUrl = "";
	var _div1 ="";
	var _div2 =""
	var _activeDiv = "";
	var _inTransition =false;
	this.init = function (div){
		var originalContent = $(div).html();
		$(div).html('<div class="bale-imageLoading" style="display:none"> </div><div class="div-1">'+originalContent+'</div><div class="div-2"></div>');
		_div1 = $(div).children(".div-1");
		_div2 = $(div).children(".div-2");
		_div2.html("<img src='images/spacer.gif' />");
		_div2.hide();
		_activeDiv =_div1;
		//_activeDiv ="cow";
		_currentPhotoUrl =_div1.children("a").children("img").attr('src'); 
		//_currentPhotoUrl =_div1.children("img").attr('src'); 
	}
	function showLoading(){
		$('.bale-imageLoading').css('display','block');
	}
	
	function hideLoading(){
		$('.bale-imageLoading').css('display','none');
	}
	
	this.switchPhoto = function(largePhotoUrl,directSwitch){
		if((_currentPhotoUrl != largePhotoUrl)&& _inTransition ==false){
			_inTransition= true;
		 $(function () {
	        var img = new Image();
        	$(img).load(function () {
            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
    	        _div2.hide();
	            _div2.html(this);
            	_div2.fadeIn(500, function(){
					_div1.html(_div2.html());
					_div1.show();
					_div2.hide();
					_inTransition= false;
					hideLoading();
				});
				
    	    }).error(function () {
        	    // notify the user that the image could not be loaded
	       	 }).attr('src', largePhotoUrl);
    		});
		 _currentPhotoUrl = largePhotoUrl;
		}
	}
	/*		
		alert(largePhotoUrl != largePhotoUrl);
			
			if(directSwitch){
				console.log('new url ' +newUrl);
				_div1.children('img').attr('src', largePhotoUrl);
				_div1.fadeTo(1 , 1);
				_div2.fadeTo(1 , 0);
				_activeDiv = _div1;
			}else{
					alert('activeDiv == 1');
				if(_activeDiv == _div1){
					alert('activeDiv == 1');
					showLoading();
					console.log(this.callOut);
					_div2.image(largePhotoUrl,this.callOut);
				/*	_div2.image(largePhotoUrl,function(){
						hideLoading();
					//	alert("image loaded");
						_div1.fadeTo(_fadeSpeed , 0);
						_div2.fadeTo(_fadeSpeed , 1);
						_activeDiv = _div2;
					});*/
		/*		}else{		
	
					alert('activeDiv == 1');
					showLoading();
					_div1.image(largePhotoUrl,function(){
						hideLoading();
					//	alert("image loaded");
						_div2.fadeTo(_fadeSpeed , 0);
						_div1.fadeTo(_fadeSpeed , 1);
						_activeDiv = _div1;
					});		
				}
			}
			_currentPhotoUrl = largePhotoUrl;
		}*/
//	}
	
	/*
	this.switchPhoto = function(largePhotoUrl,directSwitch){
		alert(largePhotoUrl);
		if(_currentPhotoUrl != largePhotoUrl){
			if(directSwitch){
				//console.log('new url ' +newUrl);
				_div1.children('img').attr('src', largePhotoUrl);
				_div1.show();
				_div2.hide();
				_activeDiv = _div1;
			}else{
				var inActiveDiv = _div1;
				if(_activeDiv == _div1){ 
					inActiveDiv = _div2;
					_activeDiv = _div1
				}
				showLoading();
				inActiveDiv.image(largePhotoUrl,function(){
					hideLoading();
					console.log('********')
						alert('fading-out');
					
					console.log(_activeDiv)
					console.log(inActiveDiv)
					
					console.log('********')
					inActiveDiv.fadeIn(600);
					_activeDiv.fadeOut(1200, function(){
						alert('fading-out');
						_activeDiv = inActiveDiv;
					});
				});
			}
			_currentPhotoUrl = largePhotoUrl;
		}
		hideLoading()
	}*/
}

function baleScrollableSelect(elm){
	
		//	console.log(elm);
		//	console.log(elm);
			var isScrollable
			
			orgin = $(elm);
			orginName = orgin.attr('id');
			if(!orginName) orginName = orgin.attr('name');
			
			var orginWidth = orgin.css('width');
			var orginHeight = orgin.css('height');
			
			orginWidth = orginWidth.split('px')[0];
			orginHeight =orginHeight.split('px')[0];
			
			var sAreaName=orginName+"SArea";
			var sTextName = orginName+"SText";
			var anchorSelectName=orginName+"SAnchor";
			var optionsDivName=orginName+"SOptions";
			
			var selectValues = new Array();
			var displayValues = new Array();
			orgin.children().each(function(){
				selectValues.push($(this).attr('value'));
				displayValues.push($(this).text());
			});
			
			var newContent ="<div class='selectAreaHolder'  style='";
			
			if (orginHeight) newContent += "height:"+orginHeight+"px;";
			if (orginWidth) newContent += "width:"+orginWidth+"px;";
			
			newContent += "'>";
			newContent += "<div class='selectArea'id='"+sAreaName+"'   style='";
			if (orginWidth) newContent += "width:"+orginWidth+"px;";
			newContent += "'>";
			newContent += "<div class='bale-left' />";
			newContent += "<div class='bale-right' > </div>";
						newContent += "<a href='#' class='selectButton' style='";
	
			if (orginHeight) newContent += "height:"+orginHeight+"px;";
			if (orginWidth)  newContent += "width:"+(Number(orginWidth))+"px;";
			newContent += "' id='"+anchorSelectName+"'> </a>";
			
			newContent += "<div id='"+sTextName+"' class='bale-center' style='width:"+orginWidth+"px;' >"+orgin.children("option:selected").text()+"</div></div>";

			newContent +="<div class='bale-regularHidden' id='"+optionsDivName+"' style='";
			if (orginWidth) newContent += "width:"+(orginWidth-1)+"px;margin-top:-2px;";
			newContent += "'><div class='bale-selectScrollDiv'  style='";
			if (orginWidth) newContent += "width:"+(orginWidth-1)+"px;";
			newContent += "'>"
	
		/*	for (i=0; i<selectValues.length;i++){
				newContent += "<p><a href='#' parentTarget='"+ orginName +"' style=width:"+(orginWidth)+"px;' referanceValue='"+ selectValues[i] +"'>"+displayValues[i]+"</a></p>";
			}
		*/
			
			
			for (i=0; i<selectValues.length;i++){
				newContent += "<p><a href='#' parentTarget='"+ orginName +"' referanceValue='"+ selectValues[i] +"'>"+displayValues[i]+"</a></p>";
			}
			newContent +="</div></div></div>";
			orgin.after(newContent); 
			
			$('#'+anchorSelectName).click(function(){
				$('#'+optionsDivName).toggleClass('bale-regularShowingScroll')
				//console.log($('#'+optionsDivName + " div"));
				var thisQuery= $(this);
			//	alert($('#'+optionsDivName + " .bale-selectScrollDiv").css('width'));
				if(thisQuery.attr('scrollInit') != 'true'){
					
					$('#'+optionsDivName + " .bale-selectScrollDiv").jScrollPane({scrollbarWidth :10});
					thisQuery.attr('scrollInit',"true");
				}
				
			}).blur(function(){
				//var evalString ="$('#"+optionsDivName+"').removeClass('bale-regularShowingScroll')";
				//setTimeout(evalString,150);
			})
			
			$('#'+optionsDivName+' a').click(function(){
				$('#'+sTextName).text($(this).text());
				$('#'+optionsDivName).removeClass('bale-regularShowingScroll');
				var orgin = $('#'+$(this).attr('parentTarget'));
				orgin.selectOptions($(this).attr('referanceValue'));
				if(orgin[0].onchange)orgin[0].onchange();
			})
			orgin.hide();
}

function balePrettySelect(elm){
				orgin = $(elm);
			orginName = orgin.attr('id');
			if(!orginName) orginName = orgin.attr('name');
			
			var orginWidth = orgin.css('width');
			var orginHeight = orgin.css('height');
			
			orginWidth = orginWidth.split('px')[0];
			orginHeight =orginHeight.split('px')[0];
			
			var sAreaName=orginName+"SArea";
			var sTextName = orginName+"SText";
			var anchorSelectName=orginName+"SAnchor";
			var optionsDivName=orginName+"SOptions";
			
			var selectValues = new Array();
			var displayValues = new Array();
			orgin.children().each(function(){
				selectValues.push($(this).attr('value'));
				displayValues.push($(this).text());
			});
			
			var newContent ="<div class='selectAreaHolder'  style='";
			
			if (orginHeight) newContent += "height:"+orginHeight+"px;";
			if (orginWidth) newContent += "width:"+orginWidth+"px;";
			
			newContent += "'>";
			newContent += "<div class='selectArea'id='"+sAreaName+"'   style='";
			if (orginWidth) newContent += "width:"+orginWidth+"px;";
			newContent += "'>";
			newContent += "<div class='bale-left' />";
			newContent += "<div class='bale-right' > </div>";
						newContent += "<a href='#' class='selectButton' style='";
	
			if (orginHeight) newContent += "height:"+orginHeight+"px;";
			if (orginWidth)  newContent += "width:"+(Number(orginWidth))+"px;";
			newContent += "' id='"+anchorSelectName+"'> </a>";
			
			newContent += "<div id='"+sTextName+"' class='bale-center' style='width:"+orginWidth+"px;' >"+orgin.children("option:selected").text()+"</div></div>";

			newContent +="<div class='bale-regularHidden' id='"+optionsDivName+"' style='";
			//if (orginWidth) newContent += "width:"+(orginWidth-10)+"px;";
			if (orginWidth) newContent += "width:"+(orginWidth-3)+"px;margin-top:-1px;";
			newContent += "'>";
	
			for (i=0; i<selectValues.length;i++){
				newContent += "<p><a href='#' parentTarget='"+ orginName +"' referanceValue='"+ selectValues[i] +"'>"+displayValues[i]+"</a></p>";
			}
			newContent +="</div></div>";
			orgin.after(newContent); 
			
			$('#'+anchorSelectName).click(function(){
				$('#'+optionsDivName).toggleClass('bale-regularShowing');
				return false;
			}).blur(function(){
				var evalString ="$('#"+optionsDivName+"').removeClass('bale-regularShowing')";
				setTimeout(evalString,500);
			})
			
			$('#'+optionsDivName+' a').click(function(){
				$('#'+sTextName).text($(this).text());
				$('#'+optionsDivName).removeClass('bale-regularShowing');
				var orgin = $('#'+$(this).attr('parentTarget'));
				orgin.selectOptions($(this).attr('referanceValue'));
				if(orgin[0].onchange)orgin[0].onchange();
				return false;
			})
			orgin.hide();
}


function fixHeight(){
	if(window.innerHeight < 562){
		$('#bale-mainContainer').css({ top:"0px", marginTop:"5px" });	
	}else{
		$('#bale-mainContainer').css({ top:"50%", marginTop:"-290px" });
	}
}

$(document).ready(function(){
	fixHeight();
	//baleNavBlock.init("#bale-mainNav");
	
	//set pretty dropdowns
	$('select.bale-prettySelect').each(function(){balePrettySelect(this);});

		//set pretty dropdowns
	$('select.bale-prettySelectScroll').each(function(){baleScrollableSelect(this);});
	
	$('select.bale-prettySelectPossibleScroll').each(function(){
		if ($(this).children().length < 8){
			balePrettySelect(this);
			console.log('non-scrolling');
		}else{
			baleScrollableSelect(this);
			console.log('***scrolling');
		}
	});
});