jQuery(document).ready(function() {
	var delay = 400;

    $("#carousel").jcarousel({
    	scroll: 1,
    	offset: 1,
    	start: 2,
    	initCallback: function (carousel) {
    		function checkKey(e){
    		     switch (e.keyCode) {
    		        case 37:
    		        	if (carousel.first != 2) {
    		        		carousel.prev();
    		        	}
    		            break;
    		        case 39:
    		        	if (!$('[jcarouselindex=' + (carousel.first + 1) + ']').attr('class').match('empty')) {
    		        		carousel.next();
    		        	}
    		            break;
    		     }
    		}

    		if ($.browser.mozilla) {
    		    $(document).keypress(checkKey);
    		} else {
    		    $(document).keydown(checkKey);
    		}
    	},
    	itemFirstInCallback: {
			onBeforeAnimation: function (carousel, item, idx, state) {
    			item = $(item);
		    	item.find('.image1').stop().animate({
	    			left: '-20px',
	    			width: '200px', 
	    			height: '241px',
	    			opacity: '100'
	    		}, delay); 
		    	item.find('.image2').stop().animate({
	    			left: '-20px',
	    			width: '200px', 
	    			height: '241px',
	    			opacity: '0'
	    		}, delay); 
		    	item.find('.reflection').stop().animate({
	    			left: '-20px',
	    			width: '200px', 
	    			top: '241px',
	    			opacity: '0'
	    		}, delay); 
	    		
		    	index = '#hiddenContent .content' + $(item).attr('jcarouselindex');
		    	
		    	$('#common h1').html($(index + ' h1').html());
		    	$('#common .download-toolbar').html($(index + ' .download-toolbar').html());
		    	
		    	$('#col1_content').html($(index + ' .col1').html());
	    		$('#col2_content').html($(index + ' .col2').html());
	    		$('#col3_content').html($(index + ' .col3').html());
	    		
				$('#col1_content h2:first').css('padding-top', '0');
				$('#col2_content h2:first').css('padding-top', '0');
				$('#col3_content h2:first').css('padding-top', '0');

				item.find('.date').stop().animate({
	    			top: '259px',
	    			opacity: '0'
	    		}, delay);

        		$('#col1').height('auto');
        		$('#col2').height('auto');
        		$('#col3').height('auto');

        		height = Math.max($('#col1').height(), $('#col2').height(), $('#col3').height())
        		
        		$('#col1').height(height);
        		$('#col2').height(height);
        		$('#col3').height(height);
    			
    		}
    	}, 
    	itemFirstOutCallback: {
    		onBeforeAnimation: function (carousel, item, idx, state) {
    			item = $(item);
    			left = (state == 'prev') ? '28px' : '-28px';
    			item.find('.image1').stop().animate({
	    			left: left,
	    			width: '160px', 
	    			height: '192px',
	    			opacity: '0'
	    		}, delay);
    			item.find('.image2').stop().animate({
	    			left: left,
	    			width: '160px', 
	    			height: '192px',
	    			opacity: '100'
	    		}, delay);
    			item.find('.reflection').stop().animate({
	    			left: left,
	    			width: '160px', 
	    			top: '192px',
	    			opacity: '100'
	    		}, delay);
    			
    			if (item.attr('jcarouselindex') >= carousel.first) {
        			item.find('.date').stop().animate({
    	    			top: '210px',
    	    			opacity: '100'
    	    		}, delay);
    			}
		    }
    	}
    });
    
    var carousel = $('#carousel').data('jcarousel');
    
    function rotate(index)
	{
		var first = carousel.first;
    	
    	carousel.scroll(index);
    	carousel.first = index;
    	carousel.prevFirst = index - 1;
    	carousel.notify('onBeforeAnimation');
    	
    	for(i = 2; i < index; i++) {
    		$('[jcarouselindex=' + i + '] .image2').css('left', '-28px');
    		$('[jcarouselindex=' + i + '] .image1').css('left', '-28px');
			$('[jcarouselindex=' + i + '] .date').css('opacity', '0');
    	}
	}
	
	$('.jcarousel-skin-tango .jcarousel-item-horizontal').live('click', function (e){
    	if ($(this).attr('class').match('empty')) {
    		return;
    	}
    	
    	rotate(parseInt($(this).attr('jcarouselindex')));
    });

    $('.lShadow').live('click', function (e){
    	if (carousel.first != 2) {
    		carousel.prev();
    	}
    });
    
    
	$('.download-toolbar a').live('click', function(){
		link = $(this);
		type = $(this).find('span.icon').attr('class').split(' ');
		type = type[0];
		
		$.ajax({
			type: 'post',
			url : '/go2.php',
			data: { post_Id: link.attr('rel'), type: type},
			dataType : 'html',
			success: function (data) {
				window.location = link.attr('href');
			}
		});
		
		return false;
	});
	
	// newly added
	

	var flag = false;
	$('img.image2').one('load',function(){
		if (!flag){
			$.ajax({
				type: 'post',
				url : '/go3.php',
				data: { },
				dataType : 'html',
				success: function (data) {
					//alert('tmp');
					flag = true;
				}
			});
		}


		return false;
	});

	// - newly added
	
	
	if (window.location.href.indexOf('#')) {
		index = window.location.href.split('#');
		index = index[1];
		
		if (index > 2) {
			rotate(index);
		}
	}
    
});

