$(document).ready(function(){ 
	$("#socialImageFader img.a").hover(
		function() {
			$(this).stop().animate({"opacity": "0"}, "slow");
		},
		function() {
			$(this).stop().animate({"opacity": "1"}, "slow");
		}
	);
	
	
	//external links
	var renegade_host=window.location.hostname;
	$("a").live("click",function(){
		if (this.href.indexOf("mailto:") == -1) {
			if (this.hostname != renegade_host) {
				window.open(this.href);
				return false;
			}
		} else {
			//alert("email link")
		}
	});
	
	//masking the inactive images in the gallery
	$("#images_ctn").each(function(){
		//only runs on gallery page
		$("#thumbnail-cover").show();
	});
 
});

$(document).ready(function() {

	$('a.panel').click(function () {

		$('a.panel').removeClass('selected');
		$(this).addClass('selected');
		
		current = $(this);
		
		$('#images_ctn').scrollTo($(this).attr('href'), 800);		
		
		return false;
	});

	$(window).resize(function () {
		resizePanel();		
	});
	
	/*window.resize = function(event){
		width = $(window).width();
		height = $(window).height();
		
		$("#bgstretcher ul li img").css('width',width).css('height','auto');
	}*/
	
});

function resizePanel() {

	width = $(window).width();
	height = $(window).height();

	mask_width = width * $('.item').length;
		
	$('#debug').html(width  + ' ' + height + ' ' + mask_width);
		
	$('#images_ctn, .item').css({width: width, height: height});
	$('#mask').css({width: mask_width, height: height});
	$('#images_ctn').scrollTo($('a.selected').attr('href'), 0);
		
}

function setVisibility(id, value){
	document.getElementById(id).style.display=value
	//$(id).css('visibility',value);
}


//randomize background
function RandBG(){
	bgImageTotal=12;
	randomNumber = Math.ceil(Math.random()*(bgImageTotal-1))+1;

	// Initialize Backgound Stretcher
	$(document).bgStretcher({
		images: ['web_images/frontpage/sample_'+randomNumber+'.jpg'],
		imageWidth: 1024,
		imageHeight: 768,
		slideShow: false
	});

}


$(document).ready(function(){
	$("#vid_close_btn, #overlay").click(function(){
		//$("#video_ctn").replaceWith("");
		
		
		$("#overlay, #video_ctn").addClass('hide').attr("style","");
	});


$("#videolink, #videolink2, a#videolink_directors, #view_links a, .videolink, .videolink2").click(function(){
		if (this.href.indexOf("#blank")>-1) {
			return false;
		}
		$("#overlay, #video_ctn").fadeIn('fast').removeClass('hide');
		var vid = this.href;
		var vid_html=QT_GenerateOBJECTText_XHTML(vid, "772", "435", "","background","#000000","scale","tofit");

		$("#video_target").html(vid_html);
		return false;
	});


});

function buildQTobject() {
	
}

function ShuffleSubAlbums(){
	var list = new Array();	
	$("#subalbs_list .span-2 a.shuffle").each(function(){
		var item = $(this).parent();
		list.push(item);
		$(item).replaceWith();
	});
	
	list.sort(function() { return (Math.round(Math.random())-0.5); });
	
	
	
	for(var i=0; i < list.length; i++)
		$("#subalbs_list").append(list[i]);
		
}


/*
 * GRAYSCALE IMAGE CODE
 * ========================
 * 
 * from: http://webdesignerwall.com/demo/html5-grayscale/
 */

$(window).load(function(){
		
		// Fade in images so there isn't a color "pop" document load and then on window load
		$("#directors_thumbs img").animate({opacity:1},500);
		
		// clone image
		$('#directors_thumbs img').each(function(){
			var el = $(this);
			el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
				var el = $(this);
				el.parent().css({"width":this.width,"height":this.height});
				el.dequeue();
			});
			this.src = grayscale(this.src);
		});
		
		// Fade image 
		$('#directors_thumbs img').mouseover(function(){
			$(this).parent().find('img:first').stop().animate({opacity:1}, 500);
		})
		$('#directors_thumbs .img_grayscale').mouseout(function(){
			$(this).stop().animate({opacity:0}, 500);
		});		
	});
	
	// Grayscale w canvas method
	function grayscale(src){
        var canvas = document.createElement('canvas');
		var ctx = canvas.getContext('2d');
        var imgObj = new Image();
		imgObj.src = src;
		canvas.width = imgObj.width;
		canvas.height = imgObj.height; 
		ctx.drawImage(imgObj, 0, 0); 
		var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
		for(var y = 0; y < imgPixels.height; y++){
			for(var x = 0; x < imgPixels.width; x++){
				var i = (y * 4) * imgPixels.width + x * 4;
				var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
				imgPixels.data[i] = avg; 
				imgPixels.data[i + 1] = avg; 
				imgPixels.data[i + 2] = avg;
			}
		}
		ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
		return canvas.toDataURL();
    }


function check_required_fields(form1){
	var required=form1.require.value.split(",");
	var i;
	flag=true;
	$("input",form1).each(function(){
		for (i=0; i<required.length;i++) {
			if (this.name==required[i]) {
				var field = this.name;				
				field = field.replace(/_/g," ");				
				/*field = field.charAt(0).toUpperCase() + field.slice(1);*/				
				if((this.name.indexOf('email') >= 0) && (this.value.search(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)==-1)){					
					alert("Please enter valid Email");
					flag=false;
					this.focus();
					return false;
				}
				if((this.name.indexOf('phone') >= 0) && (this.value.search(/[^\d]/)!=-1)){
					alert("Please enter valid "+field);
					flag=false;
					this.focus();
					return false;
				}
				if((this.name.indexOf('postcode') >= 0) && (this.value.search(/[^\d]/)!=-1)){
					alert("Please enter valid "+field);
					flag=false;
					this.focus();
					return false;
				}
				if (this.value=="") {				
					flag=false;
					alert("Please Enter "+field);
					this.focus();
					return false;
				}
				if(this.type == "checkbox")
				{
					if(this.checked == false)
					{
						flag=false;
						alert("Please check required checkbox");
						this.focus();
						return false;
					}
				}
			}
		}
	});
	if(flag){
		$("textarea",form1).each(function(){
			for (i=0; i<required.length;i++) {
				if (this.name==required[i]) {
					var field = this.name;				
					field = field.replace(/_/g," ");				
					if (this.value=="") {				
						flag=false;
						alert("Please Enter "+field);
						this.focus();
						return false;
					}
				}
			}
		});
	}
	
	return flag;
}

$(document).ready(function(){
	$(".fadeout").hover(
		function() {
			$(this).stop().animate({"opacity": "0"}, "slow");
		},
		function() {
			$(this).stop().animate({"opacity": "1"}, "slow");
		}
	); 
});


