var mycarousel=null;
var projectImages=null;
function getLargeUrl(thumbUrl)
{
	var pos = thumbUrl.lastIndexOf(".");
	var ext = thumbUrl.substring(pos , thumbUrl.length);
	var largeUrl = thumbUrl.substring(0 , pos) + "_large" + ext;
	return largeUrl;
}
function initializeImages()
{
	
	if($('#project_details_wrapper').length )
	{
		//alert($("#project_details_thumbs img:first").length);
		var activeProject = document.location.hash.substr(1, document.location.hash.length);
		var $project = $('#projects a[id=project_'+activeProject+']');
		
		
		$project = $project.length ? $project : $('#projects a:first');
		
		$project.trigger("click");
	}
}
function slideshowInit(carousel){

	$('#projects_prev').bind('click', function() {
		carousel.prev();
		//mycarousel.prev();
		
		return false;
	});
	
	jQuery('#projects_next').bind('click', function() {
		carousel.next();
		//mycarousel.next();
		return false;
	});
}

function getProjectData(project, fullData) {
	if (fullData)
		return '<a id="'+project.id+'" href="'+project.linkUrl+'" title="'+project.title+'" client="'+project.client+'" url="'+project.url+'" desc="'+project.description+'" date="'+project.date+'" technology ="'+project.technology+'" ><img src="'+project.thumbUrl+'" /></a>';
	else
		return '<a href="'+project.linkUrl+'" title="'+project.title+'"><img src="'+project.thumbUrl+'" /></a>';
}

function getProjectIndex(projectId){
	for (var i = 0; i < projectsList.length; i++){
		if (projectsList[i].id == 'project_'+projectId)
			return i + 1;
	}
	
	return 1;
}

$(function  (){
	var isProjectsPage = $('#project_details_wrapper').length ? true : false;
	var activeProject = document.location.hash.substr(1, document.location.hash.length);
	//alert("d");
	$('.projects').jcarousel({
		scroll : 1,
		start : getProjectIndex(activeProject),
		animation : 500,
		auto : isProjectsPage ? 0 : 2,
		initCallback : slideshowInit,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		wrap : 'circular',
		itemVisibleInCallback : {
			onBeforeAnimation : function(carousel, item, i, state, evt){
				
				var idx = carousel.index(i, projectsList.length);
				var project = projectsList[idx - 1];
				if (isProjectsPage)
					carousel.add(i, getProjectData(project, true));
				else
					carousel.add(i, getProjectData(project, false));
				
				$(item).ifixpng();
			}
		},
		itemVisibleOutCallback :  {
			onAfterAnimation : function(carousel, item, i, state, evt){
				
				carousel.remove(i);
				//$(".projects").empty();
				//carousel.reset();
					
				//alert(i);
				//carousel.reset();
			}
		}
	});
	
	if (isProjectsPage) {
	 
	 	$("#paging div").live('click', function(){
				
				var $this = $(this);
				var id = $this.attr('id');
				var index = id.substr( 11 , id.length - 1 );
				//alert(index);
				//var largeUrl = getLargeUrl(thumbUrl);
				var largeUrl = projectImages [index];
				$("#project_details_image").empty() . append("<img src ='"+largeUrl+"'>");
		});
		$('#projects a').live('click', function(){
			var $this = $(this);
			//alert("click");
			$.get("get_project_images.php",{name:$this.attr('title')},function(json){
			//alert("c");
				projectImages=eval(json);
				//alert(json);
				$("#paging").empty();
				var count = projectImages.length;
				var width = count*25;
				
				for(i=0;i<count;i++)
				{
						var element =  "<div class = 'page' id='paging_img_" + i + "'>"+"</div>";// eval( i + 1 ) + 
						$("#paging").append(element);
						//alert(element);
				}
				
				$(".pagingInner").css("width", width+"px");
			});
			
			
			$('.project_details_box_left h3').html($this.attr('title'));
			$('.project_details_box_left h3').html($this.attr('title'));
			
			$('#project_details_description').empty().html($this.attr('desc'));
			$('#project_details_date span').empty().html($this.attr('date'));
			$('#project_details_technology span').empty().html($this.attr('technology'));
			$('#project_details_client span').empty().html($this.attr('client'));
			$('#project_details_url span').empty().append($('<a>').attr({"href":$this.attr('url'), "target":"_blank"}).html($this.attr('url')));
			
			var thumbUrl = $this.children().attr('src');
				//alert(thumbUrl);
			var largeUrl = getLargeUrl(thumbUrl);
				
			$("#project_details_image").empty() . append("<img src ='"+largeUrl+"'>");
			//$("#project_details_thumbs img:first").trigger("click");
			//initializeImages();
			/*
			var thumbImagePath = $this.children().attr('src');
			var bigImagePath = thumbImagePath.substr(0, thumbImagePath.length - 4) + '_large.jpg';
			$('#project_details_image').empty().append($('<img>').attr('src', bigImagePath));
			*/
			
			
			//$(".projects").empty();
	/*		projectsList.push( {
	id : 'project_vanaryan',
	thumbUrl : 'images/projects/vanaryan.jpg',
	title : 'Vanaryan',
	client : '&quot;Vanaryan&quot;',
	url : 'http://www.vanaryan.am/',
	description : 'The first books under this logotype were published in 1993.&lt;br&gt;&lt;br&gt;The printing establishment was officially recorded in 1996. Vanaryan is one of the first private printing houses with its own printing base. Vanaryan got its  good reputation by printing of more than 650 named books of different types (poetry, historical, dramaturgy, art criticism, lexical, scientific etc.)',
	linkUrl : 'index.php?page=projects#vanaryan'
});*/
			//$(".projects").jcarousel().reset();
			
	/*		for(i=0;i<projectsList.length;i++)
			{
				$(".projects").jcarousel().add(i,getProjectData(projectsList[i], true));//append("<li>"+getProjectData(projectsList[i], true)+"</li>");
				
			}*/
			//$(".projects").jcarousel();
			//var carousel = $("#projects").data('jcarousel');
			
			
			//alert( carousel );
			//return false;
			//mycarousel.reset();
			//return false;
		});
		
		window.onload = function(){
			var $project = $('#projects a[id="project_'+activeProject+'"]');
			//alert( $project.attr("href") );
		
			$project = $project.length ? $project : $('#projects a:first');
		
			$project.trigger("click");
		}
	}
	
	if ($('.top_main_services').length)
	{
		$('.top_main_services').kwicks({
			min : 60,
			spacing : 2,
			duration : 300,
			//sticky : true,
			onFinish : function(){
				$(this).siblings().find('.left').removeClass('left_active').end().end().find('.left').addClass('left_active');
			},
			onRestore : function(){
				$(this).siblings().find('.left').removeClass('left_active').end().end().find('.left').removeClass('left_active');
			}
		});
	}
	//initializeImages();
});
 

