
$(document).ready(function(){
	
var submenuitemh = 20;
var subMenuPhotographerH = 60;
var subMenuPhotographerM = 20;
var submenuitemmargin = 20;
var submenucount;
var submenuh;
var currentSubMenuItem;
var currentSubMenuPanel;
var currentBio;

var currentpage = "main";
var galleriesmenuexpanded = false;
var dbgalleries;
var dbImages;
var current_galleryOBJ;

var aboutMTop = -'205';
var clientsMTop = '-180';
var contactMTop = '-131';
var current_categoryButton;

var currentGalleryArray;




$('#lookLogo').children('img').attr('src', $('#lookLogo').attr('href'));
$('#lookLogo img').load(function() {
	$("#lookLogo").fadeIn('slow');
	// addHomeContent();
});

$("#menuItem1").delay(0).fadeIn('slow');
$("#menuItem2").delay(150).fadeIn('slow');
$("#menuItem3").delay(300).fadeIn('slow');
$("#menuItem4").delay(450).fadeIn('slow');
$("#menuItem5").delay(600).fadeIn('slow');
$("#ub_link").delay(750).fadeIn('slow');
$("#cc").delay(900).fadeIn('slow');




// ONE TIME SAMPLE

$(this).oneTime(1500, function() {
   addHomeContent();
});


// addHomeContent();
buttonize();
relocatemain();

function addHomeContent() {
	// $('#home').children('img').attr('src', $('#home').attr('href'));
	// $('#home img').load(function() {
		$("#home").fadeIn(1000);
	// });
}


function buttonize() {	

	$('#menuItem1').click(function() {
		changepage("galleries");
		// graymenuclass('#menuItem1');
	});
	
	$('#menuItem2').click(function() {
		changepage("aboutus");
		graymenuclass('#menuItem2');
	});
	
	$('#menuItem3').click(function() {
		changepage("clients");
		graymenuclass('#menuItem3');
	});
	
	$('#menuItem4').click(function() {
		changepage("blog");
		graymenuclass('#menuItem4');
	});
	
	$('#menuItem5').click(function() {
		changepage("contact");
		graymenuclass('#menuItem5');
	});
	
	$('.categoryButton').click(function() {
		// alert($(this).attr('href'))
		
		if($(this).hasClass('liBio')){
			
			 $($(this).attr('href')).fadeIn();
			 $($(this).attr('href')).children('div').fadeIn();
			
		}else{
		
			delayTime = 0;



/*                                      */		
/*                                      */
/////////////////////// IMAJLAR EKLENIYOR
/*                                      */
/*                                      */
		
		// if(currentGalleryArray){stopGallery(currentGalleryArray);}
			// currentGalleryArray = $($(this).attr('href'));
			$(window).scrollTo( 0, 0, {duration:1000} );
			loadGallery($($(this).attr('href')).children('div'));
/* 			$($(this).attr('href')).children('div').switchClass( "photographContainerVisible", "photographContainer", 1000 ); */
		
		}
		
		delayTimeC = 0;
		
		if(current_galleryOBJ){
		
		
		
		////////////////////////////////////////////

			current_galleryOBJ.children('div').each(function(i) {
			
/* 			$(this).children('img').attr('src', $(this).attr('href')); */
/* 			$(this).detach("<img class='imageoncanvas' src='" + $(this).attr('href') + "'>"); */
			current_galleryOBJ.find(".imageoncanvas").hide();
/* 			$(this).remove("<img class='imageoncanvas'>"); */
			
 			$(this).delay(delayTime).fadeOut(); 
			delayTime+=0;
			
		});

			/*
current_galleryOBJ.children('div').fadeOut('fast', function() {
				$(this).prepend("<img class='imageoncanvas' src='" + $(this).attr('href') + "'>");
			});
*/
			
			
			current_galleryOBJ.removeClass('selectedgallery');
			
		}
		
		// $($(this).attr('href')).children().$(this).css('display', 'block');
		
		// $($(this).attr('href')).fadeIn();
		
		$(this).parent().children().filter('.selectedgallery').removeClass("selectedgallery");
		$($(this).attr('href')).addClass('selectedgallery');
		$(this).addClass('selectedgallery');
		initScrollWidth();
		current_galleryOBJ = $($(this).attr('href'));
		current_categoryButton = $(this);
	})
	
	$('.subMenuItem').click(function(){

		graymenuclass('#menuItem1');
		closecurrentpage();
		currentpage = 'galleries';
		
		if(currentSubMenuPanel){currentSubMenuPanel.switchClass( "openSubMenu", "closeSubMenu", 1000 );}
		$($(this).attr('href')).switchClass( "closeSubMenu", "openSubMenu", 1000 );
		currentSubMenuPanel = $($(this).attr('href'));
		
		
		//$('#subMenuPhotographer').animate({
		//height:100
		//}, 200, function() {
		//});
		
		$('#subMenuPhotographer').switchClass( "heightZero", "heightAuto", 1000 );       
		
		$(this).animate({
		    height: 200
		  }, 200, function() {
		    // Animation complete.
		  });

		
		if(currentSubMenuItem){
		
			$(currentSubMenuItem).animate({
			    height: 20
			  }, 200, function() {
			    // Animation complete.
			  });
		}
		$(this).css('height', '0');
		$(this).animate({
		    height: 0
		  }, 200, function() {
		    // Animation complete.
		  });
		
		$('#subMenu').animate({
			marginBottom: 0
		}, 200, function() {
		// Animation complete.
		});
		
		currentSubMenuItem = $(this);
		
		$(currentSubMenuPanel).find('ul li:first').oneTime(200, function() {
		   $(this).trigger('click');
		});
	})
	
	// $('#Bio').click(function(){
	// 		
	// 		
	// 		currentBio    = $(this);
	// 				current_categoryButton.removeClass('selected');
	// 				
	// 				delayTimeC = 0;
	// 				
	// 				if(current_galleryOBJ){
	// 				
	// 					current_galleryOBJ.children('div').each(function(i) {
	// 				
	// 						$(this).delay(delayTimeC).fadeOut();
	// 				
	// 						delayTimeC+ = 100;
	// 				
	// 					});
	// 				
	// 					current_galleryOBJ.removeClass('selected');
	// 				
	// 				}
	// 				
	// 				$($(this).attr('href')).switchClass( "closeSubMenu", "openSubMenu", 1000 );
	// 		
	// 		
	// 	})
	
}

function graymenuclass(except) {
	$(except).addClass('button_bn').removeClass('button_g').removeClass('button_b');
	if(except!='#menuItem1'){$('#menuItem1').removeClass('button_b').addClass('button_g');}
	if(except!='#menuItem2'){$('#menuItem2').removeClass('button_b').addClass('button_g');}
	if(except!='#menuItem3'){$('#menuItem3').removeClass('button_b').addClass('button_g');}
	if(except!='#menuItem4'){$('#menuItem4').removeClass('button_b').addClass('button_g');}
	if(except!='#menuItem5'){$('#menuItem5').removeClass('button_b').addClass('button_g');}
}


function relocatemain() {
	$("#main").css('marginLeft', function(index) {
	  return $('#menuWrapper').width() + 60;
	});
}

function changepage(target) {

	relocatemain();
		
	switch(target)
	{
	case 'galleries': 
		galleriesclicked();
		// galleriesmenuexpanded = true;
	break;
	case 'aboutus': 
		closecurrentpage();
		if(galleriesmenuexpanded==true){closegalleries();}
		$("#about").animate({
			marginTop: aboutMTop
		}, 200, function() {
		  // Animation complete.
		});
		$("#about").fadeIn('fast');
		$("#aboutusgallery").delay(1000).fadeIn('fast');
		
		// wwwwwwwwwwwwwwwwwwwwwwwwWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
		
		
		

			$("#aboutusgallery").children('div').each(function(i) {
				
				
				if ( $(this).find('img').length == 0 ) {
					$(this).append("<img src='" + $(this).attr('href') + "'>");
				}
			});
		
		
		
		
		
		
		
		currentpage = "aboutus";
	break;
	case 'clients': 
		closecurrentpage();
		if(galleriesmenuexpanded==true){closegalleries();}
		$("#clients").animate({
			marginTop: clientsMTop
		}, 200, function() {
		  // Animation complete.
		});
		$("#clients").show('fast');
		currentpage = "clients";
	break;
	case 'blog':
		if(galleriesmenuexpanded==true){closegalleries();}
	break;
	case 'contact':
		closecurrentpage();
		if(galleriesmenuexpanded==true){closegalleries();}
		$("#contact").animate({
			marginTop: contactMTop
		}, 200, function() {
		  // Animation complete.
		});
		$("#contact").show('fast');
		currentpage = "contact";
	break;
	}
}

function closecurrentpage() {
	
	switch(currentpage)
	{
	case 'main': 
		$("#home").fadeOut('fast');
/* 		$("#home img").fadeOut('fast'); */
	break;
	case 'galleries':
		if(current_galleryOBJ){
		
		
		///////////////
		
		

			current_galleryOBJ.children('div').fadeOut('fast', function() {
				$(this).prepend("<img class='imageoncanvas' src='" + $(this).attr('href') + "'>");
			});
			
			current_galleryOBJ.removeClass('selectedgallery');
			current_galleryOBJ = null;
			
		}
	break;
	case 'aboutus':
		$("#about").fadeOut('fast');
	break;
	case 'clients':
		$("#clients").fadeOut('fast');
	break;
	case 'contact':
		$("#contact").fadeOut('fast');
	break;
	}	
}

function galleriesclicked() {
	if(galleriesmenuexpanded==false) {
		opengalleries();
	}else{
		closegalleries();
	}
}

function opengalleries() {
	// alert('opening galleries')
	
	$('.subMenuItem').css('height', '20px');
	//$('.subMenuItem').css('height', 'auto');
	
	galleriesmenuexpanded = true;
	submenucount = $('#subMenu div').length;
	submenuh = submenuitemh*submenucount;
	$('#subMenu').animate({
		height: submenuh,
		marginTop: submenuitemmargin,
		marginBottom: submenuitemmargin
	}, 200, function() {
	// Animation complete.
	});
	
	switch(currentpage)
		{
		case 'main': 
			$("#home").css('top');
		break;
		case 'galleries':
		//
		break;
		case 'aboutus':
			// $("#about").css('top', $("#about").position().top+submenuitemmargin+submenuitemmargin+submenuh );
			var temptop = parseInt($("#about").css("marginTop"))+submenuitemmargin+submenuitemmargin+submenuh;
			$("#about").animate({
			    marginTop: temptop
			  }, 200, function() {
			    // Animation complete.
			  });
		break;
		case 'clients':
			// $("#clients").fadeOut('fast');
			var temptop = parseInt($("#clients").css("marginTop"))+submenuitemmargin+submenuitemmargin+submenuh;
			$("#clients").animate({
			    marginTop: temptop
			  }, 200, function() {
			    // Animation complete.
			  });
		break;
		case 'contact':
		var temptop = parseInt($("#contact").css("marginTop"))+submenuitemmargin+submenuitemmargin+submenuh;
		$("#contact").animate({
		    marginTop: temptop
		  }, 200, function() {
		    // Animation complete.
		  });
			// $("#contact").fadeOut('fast');
		break;
		}
}

function closegalleries() {
	// alert('closing galleries')
	// alert(galleriesmenuexpanded);
	galleriesmenuexpanded = false;
	submenucount = $('#subMenu div').length;
	submenuh = submenuitemh*submenucount;
	
		
		
		
		$('#subMenu').animate({
		    height: 0,
			marginTop: 0,
			marginBottom: 0
		  }, 200, function() {
		    // Animation complete.
		  });
	//$('#subMenuPhotographer').animate({
	//	height: 0
	//}, 200, function() {
	//});
		$('#subMenuPhotographer').switchClass( "heightZero","heightAuto", 1000 );
		
		switch(currentpage)
		{
		case 'main': 
			$("#home").css('top');
		break;
		case 'galleries':
			//
		break;
		case 'aboutus':
			// $("#about").css('top', $("#about").position().top+submenuitemmargin+submenuitemmargin+submenuh );
			var temptop = parseInt($("#about").css("marginTop"))-submenuitemmargin-submenuitemmargin-submenuh;
			$("#about").animate({
			    marginTop: temptop
			  }, 200, function() {
			    // Animation complete.
			  });
		break;
		case 'clients':
			var temptop = parseInt($("#clients").css("marginTop"))-submenuitemmargin-submenuitemmargin-submenuh;
			$("#clients").animate({
			    marginTop: temptop
			  }, 200, function() {
			    // Animation complete.
			  });
		break;
		case 'contact':
			var temptop = parseInt($("#contact").css("marginTop"))-submenuitemmargin-submenuitemmargin-submenuh;
			$("#contact").animate({
			    marginTop: temptop
			  }, 200, function() {
			    // Animation complete.
			  });
		break;
		}
		
		if(currentSubMenuPanel){currentSubMenuPanel.switchClass( "openSubMenu", "closeSubMenu", 1000 );}
		currentSubMenuPanel = null;
	
}

function buttonizephotographer(id) {
	$('#photographer'+id).click(function() {
		photographerClicked(id);
	});
}

function photographerClicked(id) {
	$("#subMenuPhotographer").fadeIn('slow');
	$("#Name").fadeIn('slow');
	$("#tagWrapper").delay(600).fadeIn('slow');
	$('#subMenuPhotographer').animate({
	    height: subMenuPhotographerH,
		marginTop: subMenuPhotographerM,
		marginBottom: subMenuPhotographerM
	}, 600, function() {
	    // Animation complete.
	});
	closecurrentpage();
	$("#galleries").show('fast');
	currentpage = "galleries";
	addPhotographs(id, 0);
}

function addPhotographs(id, galleries) {
	// <div class="a portfolio" style="width:255px;"><div class="img_desc">lorem ipsum</div><img src="img/photo_1.png"/></div>
	$.each(dbImages, function(index, value) { 
		if(dbImages[index]['photographer']==id && dbImages[index]['genre']==galleries)
		{
			// $('#subMenu').append("<div class='a portfolio' style='width:" + dbImages[index]['imagewidth'] + ";'><div class='img_desc'>" + dbImages[index]['alt'] + "</div><img src='img/" + dbImages[index]['src'] + " '/></div>");
			(dbImages[index]['src']);
		}
	});
	initScrollWidth();
}





});

