function IE6(){return (jQuery.browser.msie  && (Math.floor(jQuery.browser.version) == 6))}
function Opera(){return jQuery.browser.opera}

function fixDisplay()
{
	// fix pngs for IE6
	if(IE6()) $('#PageHeader, #ContentOuterArea').pngFix();
		
	// js fix for the content logo, otherwise the horizontal scrollbar apppears
	fixContentLogoDisplay();
	
	// portfolio bg 
	if($('#PortfolioBG').size() > 0)
	{
		$('#PortfolioBG')
			.css('opacity', 0.6);	
		$('#ContentOuterArea .bgLogo').css('zIndex', 5);
	}
}

function fixContentLogoDisplay()
{
	var availableRightSpace = ($('body').width() - $('.InnerWrapper:first').width())/2;
	var contentLogo = $('#ContentOuterArea .bgLogo');
	var diff = 391 - availableRightSpace;
	if(diff > 0)
		contentLogo.css({
					right: -391 + diff + 'px',
					width: 615 - diff + 'px'
					
				});
	contentLogo.css('display', 'block');
	$(window).resize(fixContentLogoDisplay);
}



function initPortfolioGallery()
{
	var portfolioWrapper = jQuery('.scrollable ul.scrollableItems');	
	if (portfolioWrapper.size() == 0) return;	  
	//showPortfolioLoader();
	
	// read and parse XML data
	 jQuery.ajax({
		 type: "GET",
		 url: 'assets/modules/portfolio/index.xml',
		 dataType: "xml",
		 success: function(xml)
			{
				var xmlData = jQuery(xml).find('Portfolio');
				portfolioItems = xmlData.find('>PortfolioItem');

				var portfolioSize = portfolioItems.size();
				
				portfolioItems
					.each(function(index)
					{
						var itemInfo = jQuery(this);
						
						// building slides one by one for the modiffied gallerific plugin
						var itemHTML = jQuery('<li class="scrollableItem">' +
									      '<div class="ItemGallery" index="' + index + '" url="' + itemInfo.find('GalleryFile').attr('url') + '"></div>' + 
									      '<div class="ItemDescription">' + 
									      		'<h4>' + itemInfo.find('Client').text() + '</h4>' + 
											'<h5>' + itemInfo.find('Title').text() + '</h5>' + 
										      itemInfo.find('Description').text() + 
									      '</div>' + 
									'</li>');
						itemHTML.appendTo(portfolioWrapper);
					});
					
				replaceCufonHeadings();
				activatePortfolioGallery();
					
			 },
			 error: function(err)
					{
						//hideGalleryProgressIndicator(galleryWrapper);
					}
	 });
}

function replaceCufonHeadings()
{
	Cufon.replace('.ItemDescription h4, .ItemDescription h5');
}

function activatePortfolioGallery(portfolioWrapper)
{
	var portfolio = jQuery('.scrollable')
				.scrollable({
						size:1,
						clickable: false,
						items: 'ul.scrollableItems',
						item: 'li.scrollableItem',
						loop: true,
						onBeforeSeek: function(e,i)
								{
									initScrollerGallery(i);
								},
						api: true
					});
				
	var selectionIndex = getDefaultSelectionIndex();
	if (jQuery('.scrollable li.scrollableItem').size() > selectionIndex)
	{
		portfolio.move(selectionIndex);
		if(selectionIndex == 0)
			initScrollerGallery(0);
	}
	
}

function initScrollerGallery(scrollerItemIndex)
{
	var scrollerItem = jQuery('.scrollable li.scrollableItem').eq(scrollerItemIndex);
	var itemGalleryWrapper = scrollerItem.find('.ItemGallery');
	var itemGallery = itemGalleryWrapper.find('.PhotoGallery');
	if(itemGallery.size() == 0)
	{
		var itemGalleryURL = itemGalleryWrapper.attr('url');
		if(itemGalleryURL != '')
			activatePortfolioItemGallery(itemGalleryWrapper, itemGalleryURL)
	}
}

function showGalleryProgressIndicator(gw)
{
	var progressIndicator = gw.find('>.galleryProgressIndicator');
	progressIndicator
		.css({
			 	opacity: 0.1,
			 	display: 'block'
			})
		.fadeTo('fast', 1);
}

function hideGalleryProgressIndicator(gw)
{
	gw.find('>.galleryProgressIndicator')
		.fadeTo('fast', 0.1, function(){jQuery(this).css('display', 'none')})
}

function XMLGallerific(galleryWrapper, XMLpath){
			if (galleryWrapper.size() == 0) return;
			// gallery HTML elements
			var gallery = {
						thumbnailWrapper: galleryWrapper.find('.Thumbnails ul'),
						mainImgWrapper: galleryWrapper.find('.PhotoGallery .MainItem')
					  };
					  
			showGalleryProgressIndicator(galleryWrapper);
			
			// read and parse XML data
			 jQuery.ajax({
				 type: "GET",
				 url: XMLpath,
				 dataType: "xml",
				 success: function(xml)
				 			{
								// get XML info into HTML containers
								var xmlData = jQuery(xml).find('gallery');							
								
								// get slides information
								var gallerySlidesLocation = xmlData.find('>slidesLocation').text();
								gallerySlides = xmlData.find('>slides>slide');
								
								var gallerySize = gallerySlides.size();
								var slidesLoaded = 0;
								
								// build HTML for each slide
								gallerySlides
									.each(function(index)
											{
												var slideInfo = jQuery(this);			
												
												// building slides one by one for the modiffied gallerific plugin
												var thumbnail = jQuery('<li><a href="' + gallerySlidesLocation + slideInfo.find('large').text()  + '" ' +
																		'original="' + gallerySlidesLocation + slideInfo.find('large').text() + '" ' +
																		'title="' + slideInfo.find('title').text() + '" ' +
																		'description="' + slideInfo.find('description').text() + '"' +
																		'index="' + index + '">' +
																			'<img src="' + gallerySlidesLocation + slideInfo.find('thumbnail').text() + '" />' +
																		'</a></li>');
												
												thumbnail.appendTo(gallery.thumbnailWrapper);
												
												thumbnail.find('img')
													 .load(function()
															{	
																slidesLoaded += 1;
																if(index == gallerySize - 1)
																{
																	// last slide loaded, initialize modiffied gallerific gallery							
																	initGallery(galleryWrapper);
																	hideGalleryProgressIndicator(galleryWrapper);
																}																													
															});
											}); //close each
				 },
				 error: function(err)
						{
							hideGalleryProgressIndicator(galleryWrapper);
						}
			 });
		}
		
function initGallery(galleryWrapper)
{							
	imageGallery = galleryWrapper
					.galleriffic(galleryWrapper.find('.Thumbnails'), 
								 {
									delay:                8000,
									numThumbs:            7,
									preloadAhead:         1, // Set to -1 to preload all images
									enableTopPager:       true,
									enableBottomPager:    true,
									pagerType: 	      'prevNext',
									imageContainerSel:    galleryWrapper.find('.MainItem'),
									controlsContainerSel: galleryWrapper.find('.Nav'),
									
									titleContainerSel:    galleryWrapper.find('.SlideTitle'),
									descContainerSel:     null,
									
									downloadLinkSel:      '',
									
									controlsContainerSel:   galleryWrapper.find('.SlideNavControls'),
									prevLinkText:           '&lt;&lt; back',
									nextLinkText:           'next &gt;&gt;',


									
									
									renderSSControls:     false,
									
									renderNavControls:    true,
									autostart: false
								});
}

function activatePortfolioItemGallery(wrapper, xmlUrl)
{
	wrapper.html('<div class="PhotoGallery">' +
		     	'<div class="ImageArea">' + 
			'<div class="MainItem"></div>' +
			'</div>' +
			'<div class="GalleryTitleArea">' +
				'<div class="SlideNavControls"></div>' +
				'<div class="SlideTitle"></div>' +
			'</div>' +
			'<div class="Thumbnails">' +
				'<ul></ul>' +
			'</div>' +
                        '<div class="galleryProgressIndicator">Loading...</div>' +
                '</div>');
		
		XMLGallerific(wrapper.find('>.PhotoGallery'), xmlUrl);
}

function getDefaultSelectionIndex()
{	
	var pageUrl = new String(window.location.href);
	var filter = /\?s=[0-9]+/;
	var filterMatch = new String(pageUrl.match(filter));
	if (filterMatch != 'null')
	{
		var selectionIndex = new String(filterMatch.substring(3));
		return parseInt(selectionIndex);
	}
	return 0
}

function initPortfolioListing()
{
	var portfolioListingWrapper = jQuery('#PortfolioListing ul');	
	if (portfolioListingWrapper.size() == 0) return;	  
	//showPortfolioLoader();
	
	// read and parse XML data
	 jQuery.ajax({
		 type: "GET",
		 url: 'assets/modules/portfolio/index.xml',
		 dataType: "xml",
		 success: function(xml)
			{
				var xmlData = jQuery(xml).find('Portfolio');
				portfolioItems = xmlData.find('>PortfolioItem');

				var portfolioSize = portfolioItems.size();
				portfolioItems
					.each(function(index)
					{
						var itemInfo = jQuery(this);
						var portfolioUrl = 'portfolio.html?s=' + index;
						// building slides one by one for the modiffied gallerific plugin
						var itemHTML = jQuery('<li>' +
									      '<div class="ImageWrapper">' +
									     	 '<a href="' + portfolioUrl + '"><img src="' + itemInfo.find('Thumbnail').attr('url') + '" border="0" /></a>' +
									      '</div>' +
									      '<div class="TextWrapper">' + 
									      		'<h4><a href="' + portfolioUrl + '">' + itemInfo.find('Client').text() + ' - ' + itemInfo.find('Title').text() + '</a></h4>' + 
											'<p><a href="' + portfolioUrl + '">' + itemInfo.find('Type').text() + '</a></p>' + 
									      '</div>' + 
									'</li>');
						itemHTML.appendTo(portfolioListingWrapper);
					});
					
			 },
			 error: function(err)
					{
						//hideGalleryProgressIndicator(galleryWrapper);
					}
	 });
}

jQuery(function()
{
	fixDisplay();
	initPortfolioGallery();
	initPortfolioListing();
})