/*
Design District Core JavaScript
By: Junaid Bhura
www.junaidbhura.com
*/

var imageArray = new Array(
new Object({index:1, path: 'assets/images/portfolio/brand_identity/', projects: ['navsar', 'sales_xpo', 'above', 'mobile_333', 'pramiti', 'track', 'basil']}),
new Object({index:2, path: 'assets/images/portfolio/web_design_develop/', projects: ['above', 'navsar', 'sales_xpo', 'pingtopia', 'webchutney'], urls: ['http://www.above-inc.com', 'http://www.navsar.co.in', 'http://www.salesxpo.com', 'http://www.pingtopia.com', 'assets/images/portfolio/web_design_develop/webchutney/webchutney_page.jpg']}),
new Object({index:3, path: 'assets/images/portfolio/online_marketing/', projects: ['hbo', 'google_android', 'bhasha', 'hbo_emailer'], totalDesigns: [1, 1, 1, 1]}),
new Object({index:4, path: 'assets/images/portfolio/information_graphics/', projects: ['pingtopia/1', 'pingtopia/2'], totalDesigns: [1, 1]}),
new Object({index:5, path: 'assets/images/portfolio/print_collaterals/', projects: ['69', 'oremus/brochure', 'oremus/poster', 'bhasha'], totalDesigns: [5, 4, 4, 5]})
);
var currentTab = 0;
var currentGalleryPopUp = 0;
var currentGalleryPopUpImage = 0;
var retainSizeFlag = false;
var galleryPopUpDelay = 5000;
var intID;
$(window).load(function(){
	var url = window.location.toString();
	var hashPos = url.indexOf("#");
	if(hashPos > 0) {
		switch(url.substr(hashPos + 1)) {
			case 'brand-identity': fireMenu(1); break;
			case 'web': fireMenu(2); break;
			case 'online-marketing': fireMenu(3); break;
			case 'information-graphics': fireMenu(4); break;
			case 'print': fireMenu(5); break;
		}
	}
});
function fireMenu(index) {
	if(currentTab != index) {
		$(".PortfolioContent").hide("fast");
		$("#tab" + index).show("slow", function() { fireGallery(index, 1); });
		//location.href = "#item" + index;
		sortMenus(index);
	}
}
function sortMenus(index) {
	if(currentTab == 0) {
		if(index != 1) {
			$("#item" + index).insertBefore($("#item1"));
			currentTab = index;
		}
	}
	else {
		$("#item" + index).insertBefore($("#item" + currentTab));
		currentTab = index;
	}
}
function fireGallery(mainIndex, index) {
	var len = imageArray.length;
	for(i=0; i<len; i++) {
		if(imageArray[i].index == mainIndex) {
			$('#gallery' + mainIndex).html('').addClass('AjaxLoadingCenter');
			var img = new Image();
			$(img).load(function () {
            	$(this).hide();
				$(this).attr('style', "cursor:pointer;");
            	$('#gallery' + mainIndex).removeClass('AjaxLoadingCenter').html(this);
            	$(this).fadeIn();
				moveGalleryArrow(index);
				switch(mainIndex) {
					case 2:
					if(index == 5)
						$(".InnerLink").html('<a href="' + imageArray[i].urls[index - 1] + '" target="_blank"><img src="assets/images/text_view_design.png" alt="View Design" /></a>');
					else
						$(".InnerLink").html('<a href="' + imageArray[i].urls[index - 1] + '" target="_blank"><img src="assets/images/text_launch_site.png" alt="Launch Site" /></a>');
					break;
					case 3: $(".InnerLink").html('<a href="javascript: viewDesign(3,' + index + ',1)"><img src="assets/images/text_view_design.png" alt="View Design" /></a>'); $(this).attr('onclick', 'viewDesign(3,' + index + ',1)'); break;
					case 4: $(".InnerLink").html('<a href="javascript: viewDesign(4,' + index + ',1)"><img src="assets/images/text_view_design.png" alt="View Design" /></a>'); $(this).attr('onclick', 'viewDesign(4,' + index + ',1)'); break;
					case 5: $(".InnerLink").html('<a href="javascript: viewDesign(5,' + index + ',1)"><img src="assets/images/text_view_design.png" alt="View Design" /></a>'); $(this).attr('onclick', 'viewDesign(5,' + index + ',1)'); break;
				}
        	}).error(function () {
            	alert("Could not load image.");
        	}).attr('src', imageArray[i].path + imageArray[i].projects[index - 1] + '/full.jpg');
			break;
		}
	}
}
function moveGalleryArrow(index) {
	var newPosition = ((index - 1) * 128) + 113;
	$(".GalleryArrow").css('marginLeft', newPosition + 'px');
}
function closeTab() {
	resetMenus();
	$(".PortfolioContent").hide("slow");
}
function resetMenus() {
	for(i=4; i>=1; i--) {
		$("#item" + i).insertBefore($("#item" + (i+1)));
	}
	currentTab = 0;
}
function viewDesign(mainIndex, index, popUpIndex) {
	var imageSrc;
	if(imageArray[mainIndex - 1].totalDesigns[index - 1] == 1) {
		imageSrc = imageArray[mainIndex - 1].path + imageArray[mainIndex - 1].projects[index - 1] + '/designs/design1.jpg';
		$('#galleryPopUpNavigation').html('&nbsp;');
	}
	else {
		imageSrc = imageArray[mainIndex - 1].path + imageArray[mainIndex - 1].projects[index - 1] + '/designs/design' + popUpIndex + '.jpg';
		$('#galleryPopUpNavigation').html('&nbsp;');
		var htmlString = '';
		for(i = 1; i <= imageArray[mainIndex - 1].totalDesigns[index - 1]; i++) {
			htmlString += '<a href="javascript: viewDesign(' + mainIndex + ',' + index + ',' + i + ')" id="galleryPopUpNavigation' + i + '" class="' + (popUpIndex != i? 'GalleryPopUpNavigation': 'GalleryPopUpNavigationActive') + '">&nbsp;</a>';
		}
		intID = window.clearInterval(intID);
		if(popUpIndex == imageArray[mainIndex - 1].totalDesigns[index - 1])
			intID = self.setInterval("viewDesign(" + mainIndex + ", " + index + ", 1)", galleryPopUpDelay);
		else
			intID = self.setInterval("viewDesign(" + mainIndex + ", " + index + ", " + (popUpIndex + 1) + ")", galleryPopUpDelay);
		$('#galleryPopUpNavigation').html(htmlString + '<br class="ClearFloats" />');
		$('#galleryPopUpNavigation').css('display', 'block');
	}
	if(index == imageArray[mainIndex - 1].projects.length)
		$('#nextButton').css('display', 'none');
	else
		$('#nextButton').css('display', 'block');
	if(index == 1)
		$('#previousButton').css('display', 'none');
	else
		$('#previousButton').css('display', 'block');
	var id = '#galleryPopUp';
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	currentGalleryPopUp = mainIndex;
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	$('#mask').fadeIn(200);
	$('#mask').fadeTo(200, 0.9);	
	var winH = screen.availHeight - 100;
	var winW = $(window).width();
	if(!retainSizeFlag)
		$(id).animate({'width': 600, 'height': 400}, "fast");
	$(id).css('top', winH/2-$(id).height()/2);
	$(id).css('left', winW/2-$(id).width()/2);
	$(id).show();
	if(retainSizeFlag) {
		$('#galleryPopUpImage').css('visibility', 'hidden');
		$('#galleryPopUp').addClass('AjaxLoadingCenter');
	}
	else {
		$('#galleryPopUpImage').html('&nbsp;');
		$('#galleryPopUpImage').css('width', 600);
		$('#galleryPopUpImage').css('height', 400);
		$('#galleryPopUp').css('width', 600);
		$('#galleryPopUp').css('height', 400);
		$('#galleryPopUpImage').addClass('AjaxLoadingCenter');
	}
	var img = new Image();
	$(img).load(function () {
		$(this).hide();
		$('#galleryPopUpImage').removeClass('AjaxLoadingCenter');
		$('#galleryPopUp').removeClass('AjaxLoadingCenter');
		$(this).attr('align', 'center');
		$('#galleryPopUpImage').html(this);
		$('#galleryPopUpImage').css('visibility', 'visible');
		$('#galleryPopUpImage').css('height', $(this).attr("height"));
		var temp = $(this);
		var winH = screen.availHeight - 300;
		var winW = $(window).width();
		$('#galleryPopUp').css({'style': 'text-align:center;', 'width': $(this).attr("width"), 'height': ((winW/2-$(id).width()/2)), 'top': winH/2-$(id).height()/2, 'left': winW/2-$(id).width()/2}, "slow", function() { temp.fadeIn(); });
		$('#galleryPopUp').animate({'width': $(this).attr("width"), 'height': ((winW/2-$(id).width()/2)), 'top': winH/2-$(id).height()/2, 'left': winW/2-$(id).width()/2}, "slow", function() { temp.fadeIn(); });
		currentGalleryPopUpImage = index;
	}).error(function () {
		alert("Could not load image.");
	}).attr('src', imageSrc);
}
function closeModal() {
	$('#mask').hide();
	$('#galleryPopUp').hide();
	currentGalleryPopUp = 0;
	retainSizeFlag = false;
	intID = window.clearInterval(intID);
}
function nextGalleryPopup() {
	retainSizeFlag = true;
	intID = window.clearInterval(intID);
	viewDesign(currentTab, currentGalleryPopUpImage + 1, 1);
}
function previousGalleryPopup() {
	retainSizeFlag = true;
	intID = window.clearInterval(intID);
	viewDesign(currentTab, currentGalleryPopUpImage - 1, 1);
}
