$(function() {
	//ajax example
	//$.post('?test', {email:'email',user:'user'}, function(response) {
	//	alert(response);
	//});
	
	//ajax example
	//$.get('?get_featured_rant', function(response) {
		//alert(response);
	//});
	
	//each example
	//$("#frm_manage_users input[@type=checkbox]").each(function(){
		//alert($(this).val());
	//});
	$("#share-image").click(ShareImage);
	$("#email-image").click(EmailImage);
	
	$(".show-photo").click(ShowPhoto);
	$(".gallery-photo").click(GalleryPhoto);
	
	$(".load-photo").click(LoadPhoto);
	$(".filter-photo").click(FilterPhoto);
		
	$(".view-animation").click(ViewAnimation);
	$(".load-video").click(LoadVideo);
	
	$(".show-typography").click(ShowTypography);
	$(".load-typography").click(LoadTypography);
	
	//apply filter for listing
	$(".filters").click(SelectFilter);
	
	$(".remove-filter").click(RemoveFilter);
	
	//toggle mouseover-mouseout background colors for top menu
	$(".toggle-menu").mouseover(MenuOver);
	$(".toggle-menu").mouseout(MenuOut);
	
	//toggle mouseover-mouseout background colors for thumbnail items
	$(".thumbs-title").mouseover(MenuOver);
	$(".thumbs-title").mouseout(MenuOut);
	
	$(".typography-title").mouseover(MenuOver);
	$(".typography-title").mouseout(MenuOut);
	
	$(".animation-title").mouseover(MenuOver);
	$(".animation-title").mouseout(MenuOut);
	
	$(".thumbs-item").mouseover(ThumbsOver);
	$(".thumbs-item").mouseout(ThumbsOut);
	
	$(".typography-item").mouseover(TypographyOver);
	$(".typography-item").mouseout(TypographyOut);
	
	$(".animation-item").mouseover(AnimationOver);
	$(".animation-item").mouseout(AnimationOut);
	
	//toggle drop-down menu for "Follow Us"
	$(".follow-us").mouseover(FollowUs);
	$("#follow-us-drop-down").hover(function(){},FollowUsOut);
	
	//toggle drop-down menu for "Share this site"
	$(".share-this-site").mouseover(ShareThisSite);
	$("#share-this-site-drop-down").hover(function(){},ShareThisSiteOut);
	
	//toggle drop-down menu for "Share this portfolio"
	$("#share-this-portfolio").mouseover(ShareThisPortfolio);
	$("#share-portfolio").hover(function(){},ShareThisPortfolioOut);
	
	//toggle drop-down menu for "Follow Us" on the bottom of the page
	$(".follow-us-bottom").mouseover(FollowUsBottom);
	$("#follow-us-drop-down-bottom").hover(function(){},FollowUsBottomOut);
	
	//toggle drop-down menu for "Share this site" on the bottom of the page
	$(".share-this-site-bottom").mouseover(ShareThisSiteBottom);
	$("#share-this-site-drop-down-bottom").hover(function(){},ShareThisSiteBottomOut);
	
	$("input[type=submit]").mouseover(function(){
		$(this).attr("class", "button_over");										   
	});
	
	$("input[type=submit]").mouseout(function(){
		$(this).attr("class", "button");										   
	});
	
	//jquery cycle slideshow
	$("#slideshow img").mouseover(function(){
		$("#slideshow-title").css({
			'background' : '#00AFF0'						  
		});									   
	});
	
	$("#slideshow-title a").mouseover(SlideShowLinkOver);
	
	$("#slideshow img").mouseout(function(){
		$("#slideshow-title").css({
			'background' : '#222222'						  
		});									   
	});
	
	$("#slideshow-title a").mouseout(SlideShowLinkOut);
	
	if($("#slideshow").length > 0)
		$('#slideshow').cycle({
			speed:    2500,
			timeout:  5000,
			shuffle:  {left:-300, top:30},
			clip:     'zoom',
			fx:       'fade',
			fit:      '1',
			prev:   '#prev',
    		next:   '#next',
			before:   onAfter
		});
		
	$('#pauseButton').click(function() { 
    	if($(this).attr("class") == 'pause'){
			$('#slideshow').cycle('pause');
			$(this).attr("class", "resume");
		}
		else{
			$('#slideshow').cycle('resume');
			$(this).attr("class", "pause");
		}	
	});	

})

var last_image = 0;
var last_image_type = 0;
var last_filter_photo = 0;

function EmailImage(){
	$("#error_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" alt="" />');
	
	var image_url = $("#image_url").val();
	var receiver_email = $("#receiver_email").val();
	var your_name = $("#your_name").val();
	var your_email = $("#your_email").val();
	
	if(image_url == '' || image_url == null || receiver_email == '' || receiver_email == null || your_name == '' || your_name == null || your_email == '' || your_email == null){
		$("#error_message").html('<span class="red">Please complete all the fields!</span>');
		return false;	
	}
	
	$.post(HTTP + 'ajax/?email_image&sid=' + Math.random() , {email:receiver_email,url:image_url,name:your_name,sender_email:your_email}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			$("#error_message").html('<span class="green">Message sent! This window will now close.</span>');
			setTimeout('ShareImage()', 1500);
		}
		else{
			$("#error_message").html('<span class="red">' + update[1] + '</span>');	
		}
	});	
}

function ShareImage(){
	$("#error_message").html('');
	$("#share-image-main").toggle();
	$("#email-image").click(EmailImage);
}

function FilterPhoto(){
	last_filter_photo = $(this).attr("id").substr(13);	
}

function ShowPhoto(){
	last_image = $(this).attr("id").substr(6);
	if($(this).attr("class").indexOf("kid") != -1)
		last_image_type = 1;
	else
		last_image_type = 0;
}

function GalleryPhoto(){
	last_image = $(this).attr("id").substr(14);
	if($(this).attr("class").indexOf("kid") != -1)
		last_image_type = 1;
	else
		last_image_type = 0;
}

var last_animation = 0;

function ViewAnimation(){
	last_animation = $(this).attr("id").substr(10);	
}

var last_typography = 0;

function ShowTypography(){
	last_typography = $(this).attr("id").substr(11);		
}

function LoadTypography(){
	typography_id = $(this).attr("id").substr(11);	
	$.post(HTTP + 'ajax/?get_typography&sid=' + Math.random() , {id:typography_id}, function(response) {
		if(response != 0){
			var update = response.split("|||");
			
			$(".mWin").css({
				width: update[2] + 'px'
			});
			
			$("#photo-container").html(update[0]);
			$(".load-typography").click(LoadTypography);
		}
	});	
}

function LoadPhoto(){
	photo_id = $(this).attr("id").substr(6);	
	$.post(HTTP + 'ajax/?get_image&sid=' + Math.random() , {id:photo_id,type:last_image_type}, function(response) {
		if(response != 0){
			var update = response.split("|||");
			
			if(parseInt(update[2]) < 450)
				update[2] = 450;
			
			$(".mWin").css({
				width: update[2] + 'px'
			});
			
			$("#photo-container").html(update[0]);
			$("#paginatie-modal").html(update[1]);
			
			//delete old form
			$("#share-image-main").remove();
			
			$("#modal-container").prepend(update[3]);
			
			var dif = (update[2] - 430) / 2;
			dif = parseInt(dif);
			$("#share-image-main").css({
				'left': dif + 'px'						   
			});
			
			$(".load-photo").click(LoadPhoto);
			$("#share-image").click(ShareImage);
			$("#email-image").click(EmailImage);
		}
	});	
}

function LoadVideo(){
	video_id = $(this).attr("id").substr(10);	
	$.post(HTTP + 'ajax/?get_animation&sid=' + Math.random() , {id:video_id}, function(response) {
		if(response != 0){
			var update = response.split("|||");
			$("#photo-container").html(update[0]);
			$("#paginatie-modal").html(update[1]);
			$(".load-video").click(LoadVideo);
			
			if($("embed").length > 0){
				$("embed").attr("width", 540);
				$("embed").attr("height", 468);
		
				$("embed").css({
					'width': '540px',
					'height': '468px'
				});	
			}
			
			if($("object").length > 0){
				$("object").attr("width", 540);
				$("object").attr("height", 468);
		
				$("object").css({
					'width': '540px',
					'height': '468px'
				});	
			}
			
			if($("param[name=width]").length > 0){
				$("param[name=width]").attr("value", 540);
			}
			
			if($("param[name=height]").length > 0){
				$("param[name=height]").attr("value", 468);
			}
			
			if($("iframe").length > 0){
				$("iframe").attr("width", 540);
				$("iframe").attr("height", 468);
		
				$("iframe").css({
					'width': '540px',
					'height': '468px'
				});	
			}
		}
	});	
}

function RemoveFilter(){
	filter_id = $(this).attr("id").substr(7);
	
	//animation and typography do not require filters; redirect to home page
	var url = String(document.location).toUpperCase();
	
	$.post(HTTP + 'ajax/?remove_filter', {id:filter_id}, function(response) {
		if(response == 1){
			$("#" + filter_id).removeClass("filter-selected");
			$("#remove_" + filter_id).remove();
			if( (url.indexOf("ANIMATION") != -1) || (url.indexOf("TYPOGRAPHY") != -1) || (url.indexOf("PORTFOLIO") != -1) )
				document.location = HTTP;
			else
				location.reload(true);
		}
	});
}

function SelectFilter(){
	filter_id = $(this).attr("id");
	
	//animation and typography do not require filters; redirect to home page
	var url = String(document.location).toUpperCase();
		
	$.post(HTTP + 'ajax/?select_filter', {id:filter_id}, function(response) {
		if(response == 1){
			$("#" + filter_id).addClass("filter-selected");
			if( (url.indexOf("ANIMATION") != -1) || (url.indexOf("TYPOGRAPHY") != -1) || (url.indexOf("PORTFOLIO") != -1) )
				document.location = HTTP;
			else
				location.reload(true);
		}
	});
}

function SlideShowLinkOver(){
	$("#slideshow-title").css({
		'background' : '#00AFF0'						  
	});	
}

function SlideShowLinkOut(){
	$("#slideshow-title").css({
		'background' : '#222222'						  
	});	
}

function onAfter() { 
    photo_title = $(this).find("img").attr("alt");
	illustrator_url = $(this).attr("href");
	
	$('#slideshow-title').html('<a href="' + illustrator_url + '">' + photo_title + '</a>'); 
	
	//reload triggers
	$("#slideshow-title a").mouseover(SlideShowLinkOver);
	$("#slideshow-title a").mouseout(SlideShowLinkOut);
}

function AnimationOver(){
	$(this).find(".animation-title").addClass("selected");	
}

function AnimationOut(){
	$(this).find(".animation-title").removeClass("selected");	
}

function TypographyOver(){
	$(this).find(".typography-title").addClass("selected");	
}

function TypographyOut(){
	$(this).find(".typography-title").removeClass("selected");	
}

function ThumbsOver(){
	$(this).find(".thumbs-title").addClass("selected");	
}

function ThumbsOut(){
	$(this).find(".thumbs-title").removeClass("selected");	
}

function ShareThisSite(){
	$("#follow-us-drop-down").css({
		'display': 'none'			   
	});
	
	$('#share-this-site-drop-down').css({
		'display': 'block'			
	});
}

function ShareThisSiteOut(){
	$('#share-this-site-drop-down').css({
		'display': 'none'			
	});
}

function ShareThisPortfolio(){
	$('#share-portfolio').css({
		'display': 'block'			
	});
}

function ShareThisPortfolioOut(){
	$('#share-portfolio').css({
		'display': 'none'			
	});
}

function ShareThisSiteBottom(){
	$("#follow-us-drop-down-bottom").css({
		'display': 'none'			   
	});
	
	$('#share-this-site-drop-down-bottom').css({
		'display': 'block'			
	});
}

function ShareThisSiteBottomOut(){
	$('#share-this-site-drop-down-bottom').css({
		'display': 'none'			
	});
}

function FollowUs(){
	$("#share-this-site-drop-down").css({
		'display': 'none'			   
	});
	
	$('#follow-us-drop-down').css({
		'display': 'block'			
	});
}

function FollowUsOut(){
	$('#follow-us-drop-down').css({
		'display': 'none'			
	});
}

function FollowUsBottom(){
	$("#share-this-site-drop-down-bottom").css({
		'display': 'none'			   
	});
	
	$('#follow-us-drop-down-bottom').css({
		'display': 'block'			
	});
}

function FollowUsBottomOut(){
	$('#follow-us-drop-down-bottom').css({
		'display': 'none'			
	});
}

function MenuOver(){
	if($(this).attr("class").indexOf("selected") == -1)
		$(this).addClass("selected");
}

function MenuOut(){
	if($(this).attr("class").indexOf("selected") != -1)
		$(this).removeClass("selected");
}

