
function cufonStart()
{
	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('.BlockHeader');
	Cufon.replace('.BlockContent');
	Cufon.replace('.MailLink');
}

cufonStart();

jQuery( document ).ready( function(){
	$( '#ProjectFlow' ).jcoverflip({
		current: 3,
		beforeCss: function( el, container, offset ) {
			imageMove();
			$('.Current .Image').unbind('click');
			$(el).removeClass('Current');
			$(el).css('z-index',90-(1*offset));
			
				return [
				        
				        $.jcoverflip.animationElement( el.find( '.Image .Shadow' ), { height: (Math.max(10, (272-(41*offset)) )) + 'px', width: (Math.max(10, (344-(50*offset)) )) + 'px', top: Math.min(20,(23-(6*offset)))+'px', left:Math.max(15,20-(3*offset))+'px'}, { } ),
				        $.jcoverflip.animationElement( el.find( '.Image .More' ), { opacity: 0, left: '210px'}, { } ),
				        $.jcoverflip.animationElement( el, {
				    	  top: (20+(20*offset))+'px',
				    	  left: Math.max(10, ((container.width()/2) - 300 - (127*offset) ) )+'px'
				        }, { } ),
				       $.jcoverflip.animationElement( el.find( '.Image > img' ), { width: Math.max(10,369-(50*offset)-50) + 'px' }, {} ),
				       $.jcoverflip.animationElement( el.find( '.Image .Hovered' ), { height: (Math.max(10, (272-(41*offset)) )) + 'px', width: (Math.max(10, (275-(50*offset)) )) + 'px', top: Math.min(20,(23-(6*offset)))+'px', left:Math.max(15,20-(3*offset))+'px'}, { } ),
				        ];
		},
		afterCss: function( el, container, offset ){
			$('.Current .Image').unbind('click');
			$(el).css('z-index',(90-(1*offset)));
			imageMove();
			$(el).removeClass('Current');
			return [
			        $.jcoverflip.animationElement( el.find( '.Image .Shadow' ), { height: (Math.max(10, (275-(45*offset)) )) + 'px', width: (Math.max(10, (324-(50*offset)-50) )) + 'px', top:Math.min(21,(24-(7*offset)))+'px', left: (5*offset)+20 +'px'}, { } ),
			        $.jcoverflip.animationElement( el.find( '.Image .Hovered' ), { height: (Math.max(10, (275-(45*offset)) )) + 'px', width: (Math.max(10, (324-(50*offset)-50) )) + 'px', top:Math.min(18,(24-(7*offset)))+'px', left: (5*offset)+20 +'px'}, { } ),
			        $.jcoverflip.animationElement( el.find( '.Image .More' ), { opacity: 0, left: '210px'}, { } ),
			        $.jcoverflip.animationElement( el, { left: Math.min(750, ((container.width()/2) + 58 + (127*offset) ) ) +'px', top: (20+(20*offset))+'px' }, { } ),
			        $.jcoverflip.animationElement( el.find( '.Image > img' ), { width: (Math.max(10, (369-(50*offset)-50) )) + 'px' }, {} ),
			        ];
		},
		currentCss: function( el, container ){
			$(el).css('z-index',100);
			$(el).addClass('Current');
			changeSub(el);
			imageMove();
			return [
			        $.jcoverflip.animationElement( el.find( '.Image .Shadow' ), { height: 317 + 'px', width: 316 + 'px', top: '24px', left: '24px'}, { } ),
			        $.jcoverflip.animationElement( el.find( '.Image .More' ), { opacity: 1, left: '260px'}, {} ),
			        $.jcoverflip.animationElement( el, {left:(container.width( )/2 - 159 )+'px',top:0+'px'}, {} ),
			        $.jcoverflip.animationElement( el.find( '.Image > img' ), { width: '369px' }, { } ),
			        $.jcoverflip.animationElement( el.find( '.Image .Hovered' ), { height: 317 + 'px', width: 316 + 'px', top: '24px', left: '24px'}, { } ),
			        ];
		},
		stop: function () {
			$('.Current .Image').bind('click mousedown mouseup mouseenter mouseleave ',function(event) {
				
				switch (event.type) {
					case 'mouseleave':
						$(this).children(".Hovered").css('opacity',0);
					break;
					case 'mouseenter':
						$(this).children(".Hovered").css('opacity',1);
					break;
					case 'click':
						var isOpen = $('.InerBoxContent'); 
						t1 = $(this).parent().attr('id').split('_');
						if (isOpen.attr('id') != null) {
							hideMe(t1[1]);
						} else {
							showMe(t1[1]);
						}
						break;
					case 'mouseup':
						$(this).css('top', '25px');
						break;
					case 'mousedown':
						$(this).css('top', '30px');
						break;
				}
			});
		},
		time: 1000,
		titles: {
			/** 
			 *
			 * @param el - item element
			 *
			 * @return jQuery element object of the title
			 *
			 * Order for finding the title
			 * 1) An element with a class of "title"
			 * 2) The title attribute of the item
			 * 3) The alt attribute of the item
			 * 4) The first title or alt attribute of a child element of the item
			 */
			create: function( el ){
				var titleText = '';
				var title = $( [] );
				var titleEl = el.find( '.title:first' );
				if( titleEl.size( ) == 1 ){
					title = titleEl.clone( true );
					titleEl.css( 'display', 'none' );
					title.data( 'jcoverflip__origin', 'cloned' );
					title.data( 'jcoverflip__source', titleEl );
				} else if( el.attr( 'title' ) ) {
					titleText = el.attr( 'title' );
				} else if( el.attr( 'alt' ) ) {
					titleText = el.attr( 'alt' );
				} else {
					titleEl = el.find( '[title], [alt]' ).eq( 0 );
					if( titleEl.size( ) == 1 ){
						titleText = titleEl.attr( 'title' ) || titleEl.attr( 'alt' ) || '';
					}
				}
				
				if( title.size( ) ){
					title.css( { 'opacity': 0, 'display': 'block' } );
				} else {
					title = $( '<span class="title">' + titleText + '</span>' );
					title.data( 'jcoverflip__origin', 'attribute' );
				}
				return title;
			},
			destroy: function( el ){
				/*
				if( el.data( 'jcoverflip__origin' ) == 'cloned' ){
					el.data( 'jcoverflip__source' ).css( 'display', '' );
				}
				el.remove( );
				*/
			}
		},
		titleAnimateIn: function( titleElement, time, offset ){
			if( titleElement.css( 'display' ) == 'none' ){titleElement.css({opacity: 0, display: 'block'});}titleElement.stop( ).animate({opacity: 1}, time );
		},
		titleAnimateOut: function( titleElement, time, offset ){
			titleElement.stop( ).animate( {opacity: 0 }, time, function(){  } );
		}
 });
	$('.OneSub').click(function() {
		var isOpen = $('.InerBoxContent'); 
		if (isOpen.attr('id') != null) {
			t2 = isOpen.attr('id').split('_');
			hideMe(t2[1]);
		}
		
		var t = $(this).attr('id');
		t1 = t.split('_');
		setTimeout('$("#El_"+ t1[1]).trigger("click")', 500);
	});
	
	$('.Current .Image').bind('click mousedown mouseup mouseenter mouseleave',function(event) {
		
		switch (event.type) {
			case 'mouseleave':
				$(this).children(".Hovered").css('opacity',0);
			break;
			case 'mouseenter':
				$(this).children(".Hovered").css('opacity',1);
			break;
			case 'click':
				var isOpen = $('.InerBoxContent'); 
				t1 = $(this).parent().attr('id').split('_');
				if (isOpen.attr('id') != null) {
					hideMe(t1[1]);
				} else {
					showMe(t1[1]);
				}
				break;
			case 'mouseup':
				$(this).css('top', '25px');
				break;
			case 'mousedown':
				$(this).css('top', '30px');
				break;
		}
	});
	imageMove();
	subMove();
	//moreMove();
	closeMove();
});

function imageMove()
{
	$('.Image').bind('mousedown mouseup mouseleave mouseenter',function(event) {
		switch (event.type) {
			case 'mouseleave':
				$(this).children(".Hovered").css('opacity',0);
			break;
			case 'mouseenter':
				$(this).children(".Hovered").css('opacity',1);
			break;
			case 'mouseup':
				$(this).css('top', '25px');
				break;
			case 'mousedown':
				$(this).css('top', '30px');
				break;
		}
	});
}

function closeMove()
{
	$('.Less').bind('mouseenter mouseleave mouseup mousedown', function(event) {
		var el = $(this);
		
		switch (event.type) {
			case 'mousedown':
				$(this).css('top', '1px');
				break;
			case 'mouseup':
				$(this).css('top', '0px');
				break;
			case 'mouseleave':
				//el.children('.SName').css('color', '#000000');
				//el.children('.Current .SName').css('color', '#161667');
				break;
				
			case 'mouseenter':
				//$(this).css('top', '1px');
				//el.children('.SName').css('color', '#2D1AF1');
				break;
		}
	});
}

function nexrPRMove()
{
	$('.Next').bind('mouseenter mouseleave mouseup mousedown', function(event) {
		var el = $(this);
		
		switch (event.type) {
			case 'mousedown':
				$(this).css('bottom', '-18px');
				break;
				
			case 'mouseup':
				$(this).css('bottom', '-17px');
				break;
				
			case 'mouseleave':
				//el.children('.SName').css('color', '#000000');
				//el.children('.Current .SName').css('color', '#161667');
				break;
				
			case 'mouseenter':
				//$(this).css('top', '1px');
				//el.children('.SName').css('color', '#2D1AF1');
				break;
		}
	});
}

function moreMove()
{
	$('.More').bind('mouseenter mouseleave mouseup mousedown', function(event) {
		var el = $(this);
		
		switch (event.type) {
			case 'mousedown':
				$(this).css('bottom', '14px');
				break;
			case 'mouseup':
				$(this).css('bottom', '15px');
				break;
			case 'mouseleave':
				//el.children('.SName').css('color', '#000000');
				//el.children('.Current .SName').css('color', '#161667');
				break;
				
			case 'mouseenter':
				//el.children('.SName').css('color', '#2D1AF1');
				break;
		}
	});
}

function subMove()
{
	$('.OneSub').bind('mouseenter mouseleave mouseup mousedown', function(event) {
		var el = $(this);
		
		switch (event.type) {
			case 'mousedown':
				$(this).css('top', '2px');
				break;
				
			case 'mouseup':
				$(this).css('top', '0px');
				break;
				
			case 'mouseleave':
				el.children('.SName').css('color', '#000000');
				el.children('.Current .SName').css('color', '#161667');
				break;
				
			case 'mouseenter':
				el.children('.SName').css('color', '#2D1AF1');
				break;
		}
	});	
}

function changeSub(el)
{
	var t = el.attr('id');
	t1 = t.split('_');
	$('.SubPalet').children().removeClass('Current');
	$('#SEl_'+ t1[1]).addClass('Current');
	
}
function showMe(id)
{
	var el = $('#El_' + id);
	var left = el.css('left');
	if (left != '0px') {
		el.animate({
			left: '0px'
		}, 500, MCP(el));
	}
}
function hideMe(id)
{
	var el = $('#El_' + id);
	el.animate({
		left: '321px'
	},500, MCC(el)
	);
}

function nextPR(id)
{
	var el = $('#El_' + id);
	el.animate({
		left: '321px'
	},500, MCCMext(el)
	);
}

function MCP(el)
{
	var t = el.attr('id');
	t1 = t.split('_');
	el.append("<div class='InerBoxContent'><img src='./themes/dbsweb/common/CPLong.png' alt='' /><div class='inContent'><div id='inContent'></div><div class='Less' onclick='javascript:hideMe(" + t1[1] +");'></div><div class='Next' onclick='javascript:nextPR(" + t1[1] +");'></div></div></div>");
	var cont = $(".InerBoxContent");
	cont.animate({
		width: '570px',
		left: '-20px'
	},500, function () {
		el.children('.Image').children('.More').hide();
		href = oneHref.replace('[id]', t1[1]);
		closeMove();
		nexrPRMove();
		eval(href);
	});
}
function MCC(el)
{
	var cont = $(".InerBoxContent");
	cont.animate({
		width: '0px',
		left: '0px'
	},500, function() {
		cont.remove();
		el.children('.Image').children('.More').show();
	});
}
function MCCMext(el)
{
	var cont = $(".InerBoxContent");
	cont.animate({
		width: '0px',
		left: '0px'
	},500, function() {
		cont.remove();
		el.children('.Image').children('.More').show();
		nextEl = el.next();
		
		if (nextEl.attr('id') == null) {
			nextEl = $('#ProjectFlow').children().first();
			elementNum = $('#ProjectFlow').children().length; 
		} else {
			elementNum = 0;
		}
		nextEl.trigger('click');
		setTimeout('MCCMextMore("' + nextEl.attr('id') + '")', 1100 + (elementNum * 50));
	});
}

function MCCMextMore(id){$('#' + id + ' .Image .More').trigger('click');}

function jById(href,id) {
	var h=$('#'+id);
	h.load(href,{},function() {
		cufonStart();
		var pageTracker = _gat._getTracker('UA-15364235-1');
		pageTracker._trackPageview("/" + href);
		ajaxEnd(id);
	});
	ajaxStart(id);
}

function ajaxStart(id)
{
	var h=$('#'+id);
	h.parent().append('<div class=\'Loader\'></div>');
}

function ajaxEnd(id)
{
	$('.Loader').remove();
}
function ajaxRq(href,id,form_id) 
{
	var h=$('#'+id);
	h.load(href,$('#'+form_id).serializeArray());
}
