$(document).ready(function(){
	
	$('#menu li:first').css('marginLeft', 22);
	Cufon.replace('#menu li a', {hover:true,hoverables:{a:true},fontFamily: 'Avant Garde Gothic Medium SA'});
	Cufon.replace('#language a', {hover:true,hoverables:{a:true},fontFamily: 'Avant Garde Gothic Medium SA'});
	Cufon.replace('#content .column .heading', {fontFamily: 'Avant Garde Gothic Medium SA'});
	Cufon.replace('#content .column li a', {hover:true,hoverables:{a:true},fontFamily: 'Avant Garde Gothic Medium SA'});
	Cufon.replace('#categories #categoriesTitle', {fontFamily: 'Avant Garde Gothic Medium SA'});
	Cufon.replace('#categories #categoriesList li a', {hover:true,hoverables:{a:true},fontFamily: 'Avant Garde Gothic Book SA'});
	
	Cufon.replace('#content #phone', {fontFamily: 'Avant Garde Gothic Book SA'});
	Cufon.replace('#content #phone .demi', {fontFamily: 'Avant Garde Gothic Demi'});
	Cufon.replace('#content #address', {fontFamily: 'Avant Garde Gothic Book SA'});
	Cufon.replace('#content #emails a', {fontFamily: 'Avant Garde Gothic Book SA'});
	Cufon.replace('#content #emails a .demi', {fontFamily: 'Avant Garde Gothic Demi'});
	
	Cufon.replace('#content #about p', {fontFamily: 'Avant Garde Gothic Book SA'});
	
	Cufon.replace('#content .headingLink a', {hover:true,hoverables:{a:true},fontFamily: 'Avant Garde Gothic Medium SA'});
	Cufon.replace('#photographerUrl a', {hover:true,hoverables:{a:true},fontFamily: 'Avant Garde Gothic Medium SA'});
	Cufon.replace('#photoSetDetail span', {fontFamily: 'Avant Garde Gothic Medium SA'});
	Cufon.replace('#photographerUrl span', {fontFamily: 'Avant Garde Gothic Medium SA'});
	Cufon.replace('#photoSetDetail a', {hover:true,hoverables:{a:true},fontFamily: 'Avant Garde Gothic Demi'});
	
	Cufon.replace('#slideshow .slideshowItemHeader', {fontFamily: 'Avant Garde Gothic Medium SA'});
	
	Cufon.replace('#subscribe .title', {fontFamily: 'Avant Garde Gothic Medium Condensed'});
	
	$(window).resize(function() {
		$marginTop = Math.max($(window).height() - 72 - Math.max($('#content').height(), 430) - 55, 32);
		$('#footer').css('marginTop', $marginTop);
	});
	$marginTop = Math.max($(window).height() - 72 - Math.max($('#content').height(), 430) - 55, 32);
	$('#footer').css('marginTop', $marginTop);
	
	$('#footer').css('display', 'block');
	
	
	Cufon.CSS.ready(function(){
		
		
		$columnHeight = 0;
		$('#content .column').each(function(index){
			if ($(this).height() > $columnHeight){
				$columnHeight = $(this).height();
			}
		});
		$columnHeight = 430;
		if ($('#categories').length > 0){
			
			$('#content .column').css('height', $columnHeight);
			$('#categories').css('left', Math.floor($('#content .column:last').position().left + $('#content .column:last').width() - 34));
			$('#categories').css('top', ($columnHeight - $('#categories #categoriesList').height()) / 2);

			$('#categories #categoriesButton').css('top', ($('#categories #categoriesList').height() - 84) / 2);
			$('#categories #categoriesArrow').css('top', ($('#categories #categoriesList').height() - 32) / 2);

			$('#categories').css('height', $('#categories #categoriesList').height());

			$('#categories #categoriesButton').hover(
				function () {
					$(this).parent().stop();
					$(this).parent().animate({width: $('#categories #categoriesList').width() + $('#categories #categoriesList').position().left}, 300);
				},
				function () {
					$(this).parent().stop();
					$(this).parent().animate({width: 84}, 300);
				}
			);

			$('#categories #categoriesList').hover(
				function () {
					$(this).parent().stop();
					$(this).parent().animate({width: $('#categories #categoriesList').width() + $('#categories #categoriesList').position().left}, 300);
				},
				function () {
					$(this).parent().stop();
					$(this).parent().animate({width: 84}, 300);
				}
			);

			$('#categories #categoriesArrow').hover(
				function () {
					$(this).parent().stop();
					$(this).parent().animate({width: $('#categories #categoriesList').width() + $('#categories #categoriesList').position().left}, 300);
				},
				function () {
					$(this).parent().stop();
					$(this).parent().animate({width: 84}, 300);
				}
			);

			$('#categories #categoriesList li').hover(
				function () {
					$(this).children('a').addClass('hovered');
					$hoveredSlug = $(this).children('a').attr('slug');
					$('#categories #categoriesList li a').each(function(index){
						if (($(this).attr('slug') != $hoveredSlug) && (!$(this).hasClass('active'))){
							$(this).addClass('passive');
						}
					});
					Cufon.replace('#categories #categoriesList li a', {hover:true,hoverables:{a:true},fontFamily: 'Avant Garde Gothic Book SA'});
				},
				function () {
					$(this).children('a').removeClass('hovered');
					$('#categories #categoriesList li a').each(function(index){
						$(this).removeClass('passive');
					});
					Cufon.replace('#categories #categoriesList li a', {hover:true,hoverables:{a:true},fontFamily: 'Avant Garde Gothic Book SA'});
				}
			);
			
		}
		
		if ($('#upArrow').length > 0){
			$('#upArrow').css('left', Math.floor($('#content .column:last').position().left + $('#content .column:last').width() - 34) + 29);
			$('#downArrow').css('left', Math.floor($('#content .column:last').position().left + $('#content .column:last').width() - 34) + 29);
			
			if ($('#downArrow').attr('scroll') == 'page'){
				$('#downArrow a').click(function(e){
					e.preventDefault();
					if ($('.list ul').height() > (- Number($('.list').attr('step')) + 1)  * 418){
						$('.list').attr('step', $('.list').attr('step') - 1);
						$('.list ul').animate({marginTop: -4 + $('.list').attr('step') * (418)}, 300);
					}
				});
			}
			if ($('#downArrow').attr('scroll') == 'line'){
				$('#downArrow a').click(function(e){
					e.preventDefault();
					if ($('.list ul').height() > (- Number($('.list').attr('step')) + 22)  * 19){
						$('.list').attr('step', $('.list').attr('step') - 1);
						$('.list ul').animate({marginTop: -4 + $('.list').attr('step') * (19)}, 300);
					}
				});
			}
			if ($('#upArrow').attr('scroll') == 'page'){
				$('#upArrow a').click(function(e){
					e.preventDefault();
					if ($('.list').attr('step') < 0){
						$('.list').attr('step', Number($('.list').attr('step')) + 1);
						$('.list ul').animate({marginTop: -4 + $('.list').attr('step') * (418)}, 300);
					}
				});
			}
			if ($('#upArrow').attr('scroll') == 'line'){
				$('#upArrow a').click(function(e){
					e.preventDefault();
					if ($('.list').attr('step') < 0){
						$('.list').attr('step', Number($('.list').attr('step')) + 1);
						$('.list ul').animate({marginTop: -4 + $('.list').attr('step') * (19)}, 300);
					}
				});
			}
		}
		
		
		if ($('#photographer').length > 0){
			
			$('#photoSet #photoSetInner').css('width', 5000);
			$('#photoSetDup').css('height', $columnHeight);
			$('#photoSet').css('height', $columnHeight);
			
			$photoSetWidth = 0;
			
			$('#photoSet #photoSetInner img').load(function(){
				$photoSetWidth += $(this).width() + 10;
				//alert($photoSetWidth);
				$('#photoSet #photoSetInner').css('width', $photoSetWidth - 10);

			});
			$('#photoSet #photoSetInner .photo:last').css('marginRight', 0);
			$('#photoSetDup').css('height', $columnHeight);
			$('#photoSet').css('height', $columnHeight);
			
			$('#thumbnails .thumb:nth-child(21n)').css('marginRight', 0);
			
			
			$('#photoSet').hover(
				function () {
					if ($('#photoSetInner').width != 10000){
						if ($('#photoSetInner').width() > $('#photoSet').width()){
							if ($('#photoSetLeftInner').length > 0){
								if ($('#photoSetDup').attr('current') != 0){
									$('#photoSetLeftInner').css('display', 'block');
								}
								if (Number($('#photoSetDup').attr('current')) + 1 < $('#photoSetDup').attr('total')){
									$('#photoSetRightInner').css('display', 'block');
								}
								
							}
						}
					}
				},
				function () {
					if ($('#photoSetLeftInner').length > 0){
						$('#photoSetLeftInner').css('display', 'none');
						$('#photoSetRightInner').css('display', 'none');
					}
				}
			);
			
			$('#thumbnails .thumb').hover(
				function () {
					if (!$(this).hasClass('selected')){
						$(this).addClass('active');
					}
				},
				function () {
					if (!$(this).hasClass('selected')){
						$(this).removeClass('active');
					}
				}
			);
			
			
			$('#photoSetRight a').click(function(e){
				e.preventDefault();
				if ($('#thumbnails .selected').next().length > 0){
					window.location = $('#thumbnails .selected').next().children('a').attr('href');
				}
				else{
					window.location = $('#thumbnails .thumb:first').children('a').attr('href');
				}
			});
			
			$('#photoSetLeft a').click(function(e){
				e.preventDefault();
				if ($('#thumbnails .selected').prev().length > 0){
					window.location = $('#thumbnails .selected').prev().children('a').attr('href');
				}
				else{
					window.location = $('#thumbnails .thumb:last').children('a').attr('href');
				}
			});
			
			$('#photoSetRightInner a').click(function(e){
				e.preventDefault();
				$left = 0;
				$('#photoSetDup').attr('current', Number($('#photoSetDup').attr('current')) + 1);
				if ($('#photoSetDup').attr('current') == $('#photoSetDup').attr('total')){
					$('#photoSetDup').attr('current', 0);
				}
				for ($i = 0; $i < $('#photoSetDup').attr('current'); $i ++){
					$left += $('#photoSetInner .photo').eq($i).children('img').width() + 10;
				}
				$left = Math.min($left, $('#photoSetInner').width() - $('#photoSet').width());
				$('#photoSetInner').animate({marginLeft: -$left}, 500);
				
				$('#photoSetLeftInner').css('display', 'block');
				if ($('#photoSetDup').attr('current') == ($('#photoSetDup').attr('total') - 1)){
					$('#photoSetRightInner').css('display', 'none');
				}
			});
			
			$('#photoSetLeftInner a').click(function(e){
				e.preventDefault();
				$left = 0;
				$('#photoSetDup').attr('current', Number($('#photoSetDup').attr('current')) - 1);
				if ($('#photoSetDup').attr('current') < 0){
					$('#photoSetDup').attr('current', $('#photoSetDup').attr('total') - 1);
				}
				for ($i = 0; $i < $('#photoSetDup').attr('current'); $i ++){
					$left += $('#photoSetInner .photo').eq($i).children('img').width() + 10;
				}
				$left = Math.min($left, $('#photoSetInner').width() - $('#photoSet').width());
				$('#photoSetInner').animate({marginLeft: -$left}, 500);
				
				$('#photoSetRightInner').css('display', 'block');
				if ($('#photoSetDup').attr('current') == 0){
					$('#photoSetLeftInner').css('display', 'none');
				}
			});
			
			
			/*
			$('#downloadPortfolio').click(function(){
				$photographer_id = $(this).attr('photographer_id');
				$photographer_slug = $(this).attr('photographer_slug');
				
				$url = "/index.php/page/index/tr/downloadPortfolio";
				//alert('test');
				$.post( $url, { photographer_id: $photographer_id, photographer_slug: $photographer_slug },
					function( data ) {
						//alert('complete');
						//$('#subscribeResult').html("E-posta adresiniz kaydedilmiştir.");
					}
				);
				return false;
			});
			*/
			
			
		}
		
		if ($('#content .column .listing').length > 0){
			var pane = $('#content .column .listing');
			
			pane.jScrollPane();
			
			var api = pane.data('jsp');
			if ($('#content .column .listing li .selected').length > 0){
				api.scrollToY(parseInt($('#content .column .listing li .selected').position().top));
			}
		}
		
		if ($('#slideshow').length > 0){
			if ($('#categories').length > 0){
				$('#slideshow').css('width', $('#content').width() - $('#categories').position().left - 113);
			}
			else{
				$('#slideshow').css('width', $('#content').width() - $('.column').width() - 65);
			}
			$('#slideshow').css('height', $columnHeight);

			$('#slideshow .slideshowItem img').each(function(index){
				$(this).attr('height', Math.floor(($columnHeight - 8) / 2));
			});
			
			$('#slideshowInnerT').css('width', 10000);
			$('#slideshowInnerB').css('width', 10000);

			$slideshowInterval = setInterval('panSlideshow()', 50); 
			$('#slideshow').css('display', 'block');
			
			$('#slideshow .slideshowItem').hover(
				function () {
					$(this).children('.slideshowItemHeader').css('display', 'block');
				},
				function () {
					$(this).children('.slideshowItemHeader').css('display', 'none');
				}
			);
			
			$('#slideshow .slideshowItem').click(function(){
				window.location = $(this).attr('href');
			});
		}

		if ($('#emails a').length > 0){
			$('#emails a').hover(
				function () {
					$(this).css('color', '#ef3800');
					Cufon.replace('#content #emails a', {fontFamily: 'Avant Garde Gothic Book SA'});
					Cufon.replace('#content #emails a .demi', {fontFamily: 'Avant Garde Gothic Demi'});
				},
				function () {
					$(this).css('color', '#787676');
					Cufon.replace('#content #emails a', {fontFamily: 'Avant Garde Gothic Book SA'});
					Cufon.replace('#content #emails a .demi', {fontFamily: 'Avant Garde Gothic Demi'});
				}
			);
		}


		if ($('#aboutUsPhotos').length > 0){

			$('#aboutUsPhotos #aboutUsPhotosInner img').each(function(index){
				$(this).attr('height', $('#aboutUsPhotos').height());
				if ($(this).attr('width') > $('#aboutUsPhotos').width()){
					$(this).removeAttr('height');
					$(this).attr('width', $('#aboutUsPhotos').width());
				}
				else{
					$(this).css('marginLeft', ($('#aboutUsPhotos').width() - $(this).width()) / 2);
				}
			});

			$('#aboutUsPhotos #aboutUsPhotosInner').cycle({
				fx: 'fade'
			});
		}
		
		
		$('#email').focus(
			function() {  
				if (this.value == this.defaultValue){  
					this.value = '';  
				}
			}
		);

		$('#email').blur(
			function() {  
				if ($.trim(this.value) == ''){
					this.value = (this.defaultValue ? this.defaultValue : '');  
				}
			}
		);
		
		$('#subscribeSubmitter').click(function(e){
			e.preventDefault();
			$('#subscribeForm').submit();
		});
		
		$('#subscribeForm').submit(function(){
			$emailAddress = $('#email').val();
			var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
			
			if (re.test($emailAddress)){
				//success
				
				var $form = $(this),
				email = $form.find( 'input[name="email"]' ).val(),
				url = $form.attr('action');

				$.post( url, { email: email },
					function( data ) {
						$('#subscribeResult').html("E-posta adresiniz kaydedilmiştir.");
					}
				);
			}
			else{
				$('#subscribeResult').html("Lütfen e-posta adresinizi kontrol ediniz.");
			}
			if ($(window).height() < 685){
				$('html, body').animate({scrollTop:685 - $(window).height()}, 'slow'); 
			}
			
			return false;
		});
	});
	
});

function panSlideshow(){
	$marginLeftT = $('#slideshowInnerT').css('marginLeft').substring(0, $('#slideshowInnerT').css('marginLeft').length - 2);
	$marginLeftB = $('#slideshowInnerB').css('marginLeft').substring(0, $('#slideshowInnerB').css('marginLeft').length - 2);
	$('#slideshowInnerT').css('marginLeft', $marginLeftT - 2);
	$('#slideshowInnerB').css('marginLeft', $marginLeftB - 2);
	if ((Number($marginLeftT) + $('#slideshowInnerT .slideshowItem:first').width()) < 0){
		$widthD = $('#slideshowInnerT .slideshowItem:first').width();
		$first = $('#slideshowInnerT .slideshowItem:first').detach();
		$clear = $('#slideshowInnerT .clear').detach();
		$('#slideshowInnerT').append($first);
		$('#slideshowInnerT').css('marginLeft', Number($marginLeftT) + $widthD + 8 - 1);
		$('#slideshowInnerT').append($clear);
	}
	if ((Number($marginLeftB) + $('#slideshowInnerB .slideshowItem:first').width()) < 0){
		$widthD = $('#slideshowInnerB .slideshowItem:first').width();
		$first = $('#slideshowInnerB .slideshowItem:first').detach();
		$clear = $('#slideshowInnerB .clear').detach();
		$('#slideshowInnerB').append($first);
		$('#slideshowInnerB').css('marginLeft', Number($marginLeftB) + $widthD + 8 - 1);
		$('#slideshowInnerB').append($clear);
	}
}

