var curentItem = 0;
var curPage = 1;
var totalPages = 0;

var $noTroubles = jQuery.noConflict();


function loadPortfolio()
{
	curentItem = 0;
	curPage = 1;
	totalPages = Math.ceil( portfolioList.length / 6 );
	var maskHeight = $noTroubles(document).height();
	var maskWidth = $noTroubles(window).width();
	if( maskWidth < 1000 )
		maskWidth = 1000 ;
	if( maskHeight < 900 )
		maskHeight = 900;
	$noTroubles('#portfolioLoadingPlace').css({'width':maskWidth,'height':maskHeight,'display':'block'});
	$noTroubles('#portfolioLoadingPlace .close').click(function () {
		$noTroubles("#portfolioLoadingPlace").hide();
	});
	loadPortfolioList();
}

function loadPortfolioList()
{
	var html = "<ul>";
	var start = (curPage-1) * 6;
	var end = start + 5;
	for( i = start; i <= end; i=i+1 )
	{
		if( portfolioList[i] )
			html += "<li><a href=\"javascript:selectPortfolioItem("+i+");\" title=\""+portfolioList[i]['title']+"\" id=\"portfolioItem"+i+"\"><img src=\""+portfolioPreviews+portfolioList[i]['image']+"\" alt=\"\" /></a></li>";
	}
	html += "</ul>";
	$noTroubles('#portfolioListInner').css({'display':'none'});
	$noTroubles('#portfolioListInner').html( html );
	$noTroubles('#portfolioListInner').fadeIn(3000);	
	selectPortfolioItem( curentItem );
}


function selectPortfolioItem( itemId )
{
	$noTroubles('#portfolioItem').css({'display':'none'});
	$noTroubles('#portfolioItem'+curentItem ).css({'border':'2px solid transparent'});
	$noTroubles('#portfolioItem'+itemId ).css({'border':'2px solid #ff6c00'});
	curentItem = itemId;
	$noTroubles('#browserTitle').html( portfolioList[itemId]['title'] );
	$noTroubles.preloadImages(portfolioFullsize+portfolioList[itemId]['image']);
	$noTroubles('a[name=browserAddressLink]').attr('href',portfolioList[itemId]['url'] );
	$noTroubles('a[name=browserAddressLink]').html( portfolioList[itemId]['url'] );
	$noTroubles('img[name=browserContentImage]').attr( 'src', portfolioFullsize+portfolioList[itemId]['image'] );
	$noTroubles('img[name=browserContentImage]').css( 'height', portfolioList[itemId]['imageHeight']+'px' );
	$noTroubles('#itemDescription').load( portfolioPagesDir+portfolioList[itemId]['html'] );
	$noTroubles('#portfolioItem').css({'display':'block'});
	$noTroubles('.browserContent').jScrollPane({showArrows:true, scrollbarWidth:15 });
}

function walkLeft()
{
	if( curentItem > (curPage-1) * 6 )
		selectPortfolioItem( curentItem-1 );
	else if( curentItem == (curPage-1) * 6 )
	{
		if( curPage > 1 )
			curPage = curPage - 1;
		else
			curPage = totalPages;
		if( curentItem == 0 )
			curentItem = portfolioList.length - 1;
		else
			curentItem = curentItem - 1;
		loadPortfolioList();
	}
}

function walkRight()
{
	if( (curentItem < ((curPage-1) * 6 + 5 )) && ( curentItem+1 < portfolioList.length ) )
		selectPortfolioItem( curentItem+1 );
	else
	{
		if( curentItem+1 == portfolioList.length )
		{
			curPage = 1;
			curentItem = 0;
		}
		else if( curentItem+1 == ( curPage * 6 ) )
		{
			curPage = curPage + 1;
			curentItem = curentItem + 1;
		}
		loadPortfolioList();
	}
}

function enableHoverForLeftTab()
{
        $noTroubles('#player').css( {'display': 'none'} );
	$noTroubles('a[name=aleftTab1]').bind( 'mouseover', function() { $noTroubles('div[name=leftTab1]').css( { 'background': 'url(/assets/templates/advent/images/leftTabBgHover.png)' } ) } );
	$noTroubles('a[name=aleftTab1]').bind( 'mouseout', function() { $noTroubles('div[name=leftTab1]').css( { 'background': 'url(/assets/templates/advent/images/leftTabBg.png)' } ) } );
}

function disableHoverForLeftTab()
{
        $noTroubles('#player').css( {'display': 'block'} );
	$noTroubles('a[name=aleftTab1]').bind( 'mouseover', function() { $noTroubles('div[name=leftTab1]').css( { 'background': 'url(/assets/templates/advent/images/leftTabBgHover.png)' } ) } );
	$noTroubles('a[name=aleftTab1]').bind( 'mouseout', function() { $noTroubles('div[name=leftTab1]').css( { 'background': 'url(/assets/templates/advent/images/leftTabBgHover.png)' } ) } );
}

function enableHoverForRightTab( id )
{
        if( id == 2 )
              $noTroubles('#playerm').css( {'display': 'none'} );
	$noTroubles('a[name=arightTab'+id+']').bind( 'mouseover', function() { $noTroubles('div[name=rightTab'+id+']').css( { 'background': 'url(/assets/templates/advent/images/rightTabBgHover.png)' } ) } );
	$noTroubles('a[name=arightTab'+id+']').bind( 'mouseout', function() { $noTroubles('div[name=rightTab'+id+']').css( { 'background': 'url(/assets/templates/advent/images/rightTabBg.png)' } ) } );
}

function disableHoverForRightTab( id )
{
        if( id == 2 )
              $noTroubles('#playerm').css( {'display': 'block'} );
	$noTroubles('a[name=arightTab'+id+']').bind( 'mouseover', function() { $noTroubles('div[name=rightTab'+id+']').css( { 'background': 'url(/assets/templates/advent/images/rightTabBgHover.png)' } ) } );
	$noTroubles('a[name=arightTab'+id+']').bind( 'mouseout', function() { $noTroubles('div[name=rightTab'+id+']').css( { 'background': 'url(/assets/templates/advent/images/rightTabBgHover.png)' } ) } );
}

function activateToggleRight( id )
{
	$noTroubles('a[name=arightTab'+id+']').toggle(
							function() {
								disableHoverForRightTab( id );
								$noTroubles('div[name=rightTab'+id+']').css( { 'background': 'url(/assets/templates/advent/images/rightTabBgHover.png)' } );
								$noTroubles('div[name=rightTab'+id+']').animate( { 'margin-right': '245px' }, 500 );
							},
							function() {
								$noTroubles('div[name=rightTab'+id+']').animate( { 'margin-right': '0px' }, 500 );
								$noTroubles('div[name=rightTab'+id+']').css( { 'background': 'url(/assets/templates/advent/images/rightTabBg.png)' } );
								enableHoverForRightTab( id );
							}
						);	
}

 $noTroubles(document).ready(
	function()
	{
		$noTroubles('a[name=portfolioLink]').click(function(e) { loadPortfolio(); } );
		enableHoverForLeftTab();
		$noTroubles('a[name=aleftTab1]').toggle(
								function() {
									disableHoverForLeftTab();
									$noTroubles('div[name=leftTab1]').css( { 'background': 'url(/assets/templates/advent/images/leftTabBgHover.png)' } );
									$noTroubles('div[name=leftTab1]').animate( { 'margin-left': '-10px' }, 1500 );
								},
								function() {
									$noTroubles('div[name=leftTab1]').animate( { 'margin-left': '-511px' }, 1500 );
									$noTroubles('div[name=leftTab1]').css( { 'background': 'url(/assets/templates/advent/images/leftTabBg.png)' } );
									enableHoverForLeftTab();
								}
							);
		var arr = [ 1, 2, 3 ];
		for( var i in arr )
		{
			enableHoverForRightTab( arr[i] );
			activateToggleRight( arr[i] );
		}
		loadSomeLogos();
		var myFile = document.location.toString();
		if (myFile.match('#'))
		{
			var myAnchor = '#' + myFile.split('#')[1];
			if( myAnchor == '#portfolio' )
				loadPortfolio();
			if( myAnchor == '#price' )
			{
				var maskHeight = $noTroubles(document).height();
				var maskWidth = $noTroubles(window).width();
				if( maskWidth < 1000 )
					maskWidth = 1000 ;
				if( maskHeight < 900 )
					maskHeight = 900;
				$noTroubles('#priceLoad').css({'width':maskWidth,'height':maskHeight,'display':'block'});
				$noTroubles('#priceLoad .close').click(function () {
					$noTroubles("#priceLoad").hide();
				});
				$noTroubles('a[name=leftArrow]').click( function(e) { loadLowerPlan(); } );
				$noTroubles('a[name=rightArrow]').click( function(e) { loadUpperPlan(); } );
				loadPlan();
			}
		}
	}
)



