//extremecoders.com - dinesh
$(document).ready(function(){
	/******************************************************************/
	/***********************document ready start***********************/
	/******************************************************************/
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	containerHeightChange(windowHeight, 70, 130, 18);	//container height change
	
	loadContent('home'); //onload get the home page content
	$('#menuContainer ul li:first').css({
		'background':'#FFF'									
	});
	
	//highlight menu on mouse hover
	if(!IPAD && !IPHONE && !SAMSUNG) {
		$('#menuContainer ul li').mouseover(function(){
			$(this).find('div.hoverFlap').stop().animate({
					bottom: 0,
				}, 100 
			);	
			$(this).addClass('pointer');
		});
	
	
		$('#menuContainer ul li').mouseout(function(){
			//alert('li mouseout');
			if($(this).find('div').hasClass('current')){
				//do nothing if current element is hovered again
			} else {
				//hide the menu highlight on mouse out
				$(this).find('div.hoverFlap').stop().animate({
						bottom: -25+'px',
					}, 100 
				);
			}
		});
	}
	
	//highlight menu on selection
	$('#menuContainer ul li, #menuContainer ul li div').click(function(){
		if( !IPAD && !IPHONE && !SAMSUNG) {
			$(this).find('div.hoverFlap').stop().animate({
					bottom: 0,
				}, 100 
			);	
			$(this).addClass('pointer');
		}
		
		$(this).addClass('current');
		selectedMenuItemHighLight(this);
	});
	
	tip = $('#tooltip');
	
	if(!IPAD && !IPHONE && !SAMSUNG) {
		tooltip('tellAFriendIconsContainer img');
	}
	
	//startGalleria();
	var topScrollVal = totalAllowedTopScroll = upClick = downClick = 0;
	$('#scrollDown').click(function(){	//clicking this btn will move the wrapper towards up revealing lower pics
		//alert('scrollDown');	
		//alert($('.galleria-thumbnails').height());
		//upClick = upClick+1;
		var totalImageGalHeight = $('.galleria-thumbnails').height();
		var containerHeight = $("#container").height();
		var totalAllowedTopScroll = totalImageGalHeight - containerHeight;
		var clicksAllowed = Math.floor(totalImageGalHeight/containerHeight);
		//console.log('top max scroll='+totalAllowedTopScroll+', container height='+containerHeight+' , totalImageGalHeight='+totalImageGalHeight+'clicksAllowed='+clicksAllowed);
		//console.log('top position='+$('#photoGallery').css('top'));
		
		//console.log('totalAllowedTopScroll='+(-totalAllowedTopScroll) +' < '+ '(topScrollVal-containerHeight)'+(topScrollVal-containerHeight));
		if(upClick < clicksAllowed) {
			topScrollVal = topScrollVal-containerHeight;
			//console.log('top scroll='+topScrollVal);
			$("#photoGallery").stop().animate({
					position: 'absolute',
					top : topScrollVal
				}, 500 
			);	
			/*$('#photoGallery').css({
				'position':'absolute',
				'top': topScrollVal
			});*/
			upClick = upClick+1;
			downClick = 0;
		} else {
			downClick = 0;
		}
		if(downClick > 0) {
			//$('#scrollUp').unbind('click');
			//$('#scrollDown').bind('click');
			$('#scrollDown').css('border','#FFF solid 1px'); //if down clicks possible set white border to the btn
		
		} else if(downClick == 0) { //if down clicks not possible set grey border to the btn
			$('#scrollDown').css('border','#555 solid 1px'); 
		}
		if(upClick == 0) {	//if up clicks not possible set grey border to the btn
			$('#scrollUp').css('border','#555 solid 1px');
		} else if(upClick > 0) { //if up clicks possible set white border to the btn
			$('#scrollUp').css('border','#FFF solid 1px');
		}
	});
	
	$('#scrollUp').click(function(){	//clicking this btn will move the wrapper towards down revealing upper pics
		//alert('scrollUp');		
		//alert($('.galleria-thumbnails').height());
		var totalImageGalHeight = $('.galleria-thumbnails').height();
		var containerHeight = $("#container").height();
		var totalAllowedBottomScroll = totalImageGalHeight - containerHeight;
		var clicksAllowedDown = Math.floor(totalImageGalHeight/containerHeight);
		//console.log('B bottom max scroll='+totalAllowedBottomScroll);
		//console.log('B top position='+$('#photoGallery').css('top'));
		//console.log('top scroll1='+topScrollVal);
		if(downClick < clicksAllowedDown && topScrollVal != 0) {
			topScrollVal = topScrollVal+containerHeight;
			//console.log('top scroll='+topScrollVal);
			
			$("#photoGallery").stop().animate({
					position: 'absolute',
					top : topScrollVal
				}, 500 
			);	
			
			/*$('#photoGallery').css({
				'position':'absolute',
				'top': topScrollVal
			});*/
			downClick = downClick+1;
			upClick = 0;
		
		} else {
			upClick = 0;
		}
		if(downClick > 0) {
			//$('#scrollUp').unbind('click');
			//$('#scrollDown').bind('click');
			$('#scrollDown').css('border','#FFF solid 1px');
		
		} else if(downClick == 0) {
			$('#scrollDown').css('border','#555 solid 1px');
		}
		if(upClick == 0) {
			$('#scrollUp').css('border','#555 solid 1px');
		} else if(upClick > 0) {
			$('#scrollUp').css('border','#FFF solid 1px');
		}

	});
	
	$('.galleria-thumbnails , .galleria-thumbnails .galleria-image').click(function(){
		$('#scrollDown, #scrollUp').css('display','none');		
		//also hide these btns on thumbnails click is done	on galleria-1.2.5.js line 2517
	});
	
	//alert(thumb_event_type);
	$('.galleria-close').live('click', function() {
	  $('#photoGallery').css('position','relative');
	});
	
	//tell a friend icon click in the header
	//if(!IPAD && !IPHONE) {
		tellafriendIcon();
	//}
	
	//send website link to friend
	$('#sendToFriend_submit').click(function() {
		//validate send email form inputs
		result = false;
		if($.trim($('#sendToFriend_senderName').val()) == '') {
			//alert('name empty');	
			errorHightlighter('senderName');
			result = false;
		} else {
			$('#senderName').css({
				'background' : '#000',
				'color' : '#FFF',
			});
			sn = $.trim($('#sendToFriend_senderName').val());
			result = true;
		}
		//console.log('result after1'+result);
		
		if($.trim($('#sendToFriend_senderEmail').val()) == '') {
			errorHightlighter('senderEmail');
			result = false;
		} else {
			result = validateEmail($.trim($('#sendToFriend_senderEmail').val()));
			if(!result) {
				errorHightlighter('senderEmail');	
			} else {
				$('#senderEmail').css({
					'background' : '#000',
					'color' : '#FFF',
				});	
				result = true;
			}
		}
		
		//console.log('result after2'+result);
		if($.trim($('#sendToFriend_receiverName').val()) == '') {
			errorHightlighter('receiverName');
			result = false;
		} else {
			$('#receiverName').css({
				'background' : '#000',
				'color' : '#FFF',
			});
			rn = $.trim($('#sendToFriend_receiverName').val());
			result = true;
		}
		//console.log('result after3'+result);
		
		if($.trim($('#sendToFriend_receiverEmail').val()) == '') {
			errorHightlighter('receiverEmail');
			result = false;
		} else {
			result = validateEmail($.trim($('#sendToFriend_receiverEmail').val()));
			if(!result) {
				errorHightlighter('receiverEmail');	
			} else {
				$('#receiverEmail').css({
					'background' : '#000',
					'color' : '#FFF',
				});	
				result = true;
			}
		}
		//console.log('result after4'+result);
		
		//alert(result);
		if(result == true) {
			$.ajax({
				type: 'POST',
				url: 'ajaxHandler.php',
				data: 'page=sendToFriend&sn='+sn+'&se='+$.trim($('#sendToFriend_senderEmail').val())+'&rn='+rn+'&re='+$.trim($('#sendToFriend_receiverEmail').val()),
				beforeSend: function() {
                $("#mailStatus").html('<img src="images/ajax-loader.gif" border="0" alt=" " width="16" height="16"/><span>sending email...</span>');
            	},
				success: function(response) {
					//setContainerBgImg(response);
					$('#mailStatus').text(response).css({
						'background':'#000',
						'color' : '#fff',
						'padding' : '8px 10px'
					});
				}
			});
		}
	}).mouseover(function(){
		tooltip('sendToFriend_submit');	
	});
	
	//clear form values
	$('#clearForm').click(function() {
		$('#sendToFriend_senderName,#sendToFriend_senderEmail,#sendToFriend_receiverName,#sendToFriend_receiverEmail').val('');		
		$('#mailStatus').hide();
		$('#sendToFriend_senderName').focus();
	}).mouseover(function(){
		tooltip('clearForm');
	});
	
	//form close
	$('#sendToFriendClose').click(function(){
		$('#sendToFriendPopUp').css('display','none');
		
		$('#menuContainer ul li:first div').addClass('current').stop().animate({bottom: 0}, 300 );	
		$('#menuContainer ul li:first div').parent().siblings().find('div').removeClass('current').stop().animate({
			bottom: -25+'px'
		}, 100 );
		loadContent('home'); //after send to friend form close get and show the home page content
		//$('#menuContainer ul li:first div').trigger('click');
		
	});
	
	
	/******************************************************************/
	/***********************document ready end*************************/
	/******************************************************************/
});	

//on window resize
$(window).resize(function(){
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	containerHeightChange(windowHeight, 70, 130, 18);	//on resize change container height

	$("#photoGallery").stop().animate({	//on resize photo gallery thumbs scrolled to top
			position: 'absolute',
			top : 0
		}, 500 
	);	
	downClick = upClick = topScrollVal = 0; //on resize reset clicks and topscrollval to 0
	$('.galleria-stage .galleria-images .galleria-image img').removeAttr('width');	//may be not working 
	
	//to set about/video or other child div with the height of the container
	//$("#container > div").height($("#container").height());
	if($('#about').is(":visible")) {
		$('#about').css({
			'top' : get_center_position_vertical($('#container').height()+'px')
		});
	}
	
	if($('#videos').is(':visible')) {
		$('#videos').height($('#container').height());	
	}
	
	$('#photos').css({
		'height':'100%',
		'overflow':'hidden',
		
	});
	$('#photoGallery').css({
		'height':'100%',
		'width':'100%'
	});
		
	//alert('photos='+$('#photos').height()+' photoGalleryWrapper='+$('#photoGalleryWrapper').height()+' photoGallery='+$('#photoGallery').height()+'container='+$('#container').height());
});

//dynamically change container height
function containerHeightChange(windowHeight, header, footer, margin){
	//console.log('before'+parseInt($(window).height()-70-130-18));	
	$("#container").stop().animate({
		height: windowHeight-header-footer-margin
		}, 500 ,function() {
			
			$('.galleria-image').css({
				'height':windowHeight-70-130-18+'!important',
				'margin':'0 auto'					 
			});
			//console.log($('.galleria-image').height()+','+$('#container').height());	
		}
	);	
	
}

function loadContent(section) {
	
	if(section == 'home') {	//if home tab selected
		//i = 0;	//to reset galleria
		hidePlaylist(); //hide playlist if visible
		
		removeContainerBgImg();
		
		setContainerBgImg($('#Home_bg').attr('src'));
		$('#about').css('display','none');
		
		//hide photos container on home tab select
		$('#photos,#videos,#videoThumbnails,#jp_container_video_1,#photoGallery,#contact').css({
			'display':'none'		
		});
		
		//stop video player if running
		$("#jquery_jplayer_video_1").jPlayer("stop"); 
		
		//hide scroll up, scroll down btns  when in home tab
		$('#scrollDown, #scrollUp,#sendToFriendPopUp').css('display','none'); //
		
		//
		$('#menuContainer ul li:first div.hoverFlap').addClass('current');	//onload set home menu tab selected
		//selectedMenuItemHighLight($('#menuContainer ul li:first div.hoverFlap'));	//onload set home menu tab selected
		
		controlDisplayOfAudioBtn();
		
	}
	
	if(section == 'about') {	//if about tab selected
		//i = 0;	//to reset galleria 
		
		hidePlaylist(); //hide playlist if visible
		
		removeContainerBgImg();
		
		//setContainerBgImg($('#About_bg').attr('src'));
		
		$('#about').css({
			'display' : 'block',
			'top' : get_center_position_vertical($('#container').height()+'px')
		});
		
		//hide photos container on home tab select
		$('#photos,#videos,#videoThumbnails,#jp_container_video_1,#photoGallery,#contact').css({
			'display':'none'		
		});
		
		//stop video player if running
		$("#jquery_jplayer_video_1").jPlayer("stop"); 
		
		//hide scroll up, scroll down btns  when in home tab
		$('#scrollDown, #scrollUp,#sendToFriendPopUp').css('display','none');
		
		//tweak to remove the highlight of the home tab on click of other tabs
		$('#menuContainer ul li:first').css({
			'background':'#000'									
		});	
		
		//scrollBarInit('about', 305);
		
		controlDisplayOfAudioBtn();
	}
	if(section == 'photos') {	//if photos tab selected
		
		hidePlaylist(); //hide playlist if visible
		
		removeContainerBgImg();
		
		$('.scrollBtnsWrapper').css('visibility','visible');
		
		$('#photos').css({
			'height':'inherit',
			'overflow':'hidden',
			
		});
		$('#photoGallery').css({
			'height':'inherit',
			'width':'100%'
		});
		$('#photos,#photoGallery').css({
			'display':'block',
			'z-index':'100'
		});
		$('#about,#texture,#videos,#videoThumbnails,#jp_container_video_1,#contact,#sendToFriendPopUp').css({
			'display':'none'		
		});
		
		//stop video player if running
		$("#jquery_jplayer_video_1").jPlayer("stop"); 
		
		$('#scrollDown, #scrollUp').css('display','block'); //enable scroll up/down btns
		//also show these btns on full screen exit back to thumbs is done on galleria-1.2.5.js line 3073
		
		startGalleria();	//start galleria
		
		//tweak to remove the highlight of the home tab on click of other tabs
		$('#menuContainer ul li:first').css({
			'background':'#000'									
		});
		
		setTimeout("removeContainerBgImg()",100);
		//alert($('.galleria-thumbnails').height());
		//scrollBarInit('about', 235);
		controlDisplayOfAudioBtn();
	}
	if(section == 'videos') {	//if photos tab selected
		
		hidePlaylist(); //hide playlist if visible
		
		removeContainerBgImg();
		
		//if video player earlier then stop and destroy that instance of the player
		$("#jquery_jplayer_video_1").jPlayer("stop");
		$("#jquery_jplayer_video_1").jPlayer("destroy");
		$('#jp_container_video_1').css('display','none');
		//alert($('#container').height());
		$('#videos').height($('#container').height());
		$('#videos').removeClass('displaynone');
		$('#videos,#videoThumbnails').css({
			'display':'block'
		});
		
		
		$('#about,#texture,#photos,#photoGallery,#contact,#sendToFriendPopUp').css({
			'display':'none'		
		});
		
		$('#scrollDown, #scrollUp').css('display','none'); //enable scroll up/down btns
		
		//tweak to remove the highlight of the home tab on click of other tabs
		$('#menuContainer ul li:first').css({
			'background':'#000'									
		});
		
		if(IPHONE || IPAD || SAMSUNG) {
			//for touch screen devices show play button
			$('.jp-video-play').css('visibility','visible');
		}
		
		setTimeout("removeContainerBgImg()",100);
		
		controlDisplayOfAudioBtn();
	}
	
	if(section == 'contact') {	//if contact tab selected
	
		hidePlaylist(); //hide playlist if visible
		
		removeContainerBgImg();
		
		//if video player earlier then stop and destroy that instance of the player
		$("#jquery_jplayer_video_1").jPlayer("stop");
		$("#jquery_jplayer_video_1").jPlayer("destroy");
		$('#jp_container_video_1').css('display','none');
			
		$('#contact').height($('#container').height());
		$('#contact').css('display','block');
		$('#videos').addClass('displaynone');
		$('#videos,#videoThumbnails').css({
			'display':'none'
		});
		
		$('#contact').removeClass('displaynone');
		$('#about,#texture,#photos,#photoGallery').css({
			'display':'none'		
		});
		
		$('#scrollDown, #scrollUp,#sendToFriendPopUp').css('display','none'); //enable scroll up/down btns
		
		//tweak to remove the highlight of the home tab on click of other tabs
		$('#menuContainer ul li:first').css({
			'background':'#000'									
		});
		
		//setTimeout("removeContainerBgImg()",100);
		controlDisplayOfAudioBtn();
	}
	//scrollBarInit('photoGallery',parseInt($(window).height()-70-130-18));
}

//set container bg image
function setContainerBgImg(imgPath){
	$('#container').css({
		'background' :'#000000 url('+imgPath+') no-repeat ',					
	});
}	

//remove container bg image
function removeContainerBgImg() {
	$('#container').css({
		'background' :'#000000 none',					
	});
}

//high light menu item on selection
function selectedMenuItemHighLight(obj){
	if(!IPAD && !IPHONE && !SAMSUNG) {
		//console.log('def');
		//console.log('def'+obj+' '+obj.tagName);
		$(obj).stop().animate({	bottom: 0}, 100 );	
		$(obj).parent().siblings().find('div.current').removeClass('current').stop().animate({bottom: -25+'px'}, 100 );
	} 
	if(IPAD || IPHONE || SAMSUNG){
		//console.log('i'+obj+' '+obj.tagName);
		if(obj.tagName.toLowerCase() == 'div') {
			obj = obj.parent();
		}
		$(obj).find('div').stop().animate({	bottom: 0}, 100 );	
		$(obj).siblings().find('div').removeClass('current').stop().animate({bottom: -25+'px'}, 100 );
	}
	
	//tweak to remove the highlight of the home tab on click of other tabs
	$('#menuContainer ul li:first').css({
		'background':'#000'									
	});
}

function deleteOperation(deleteUrl) {
  if (confirm("Are you sure you want to delete this file ?")) {
    document.location = deleteUrl;
  }
}


/*testdiv.ontouchstart = function(e) {
    document.getElementById('mydiv').innerHTML = document.getElementById('mydiv').innerHTML + '#';
}

testdiv.ontouchmove = function(e) {
    document.getElementById('mydiv').innerHTML = document.getElementById('mydiv').innerHTML + '!';
}
*/

//tool tip display only iOS
function toolTipIpad(event, id) {
	if(IPHONE || IPAD || SAMSUNG) {
		//var targetEvent =  event.touches.item(0);  
		var targetEvent =  event;  
		var y = targetEvent.pageY + 15,
			x = targetEvent.pageX + -100;
		$('body').append(tip);
		tip.css({top: y + 'px', left: x + 'px'});
		var str = $('#'+id).attr('lang');
		tip.text(str.replace("_", " "));
		tip.show();
		tip.delay(2000);
		tip.fadeOut();
	}
}

//display tool tip for fullscreen, tell a friends btns in top right corner of the window
function tooltip(id){
	//alert('ipad'+IPAD);
	$('#'+id).mouseenter(
		function(e) {
			//alert(e.pageY+' '+e.pageX)
			var x = e.pageY + 15,
				y = e.pageX + -100;
			$('body').append(tip);
			tip.css({top: y + 'px', left: x + 'px', display: 'block'});
			var str1 = $('#'+id).attr('lang');
			tip.text(str1.replace("_", " "));
		}
	)
	.mousemove(
		function(e) {
			var y = e.pageY + 15,
			x = e.pageX + -100;
			tip.css({top: y + 'px', left: x + 'px'});
		}
	)
	.mouseleave(
		function() {
			tip.hide();
		}
	);	
}


var i = 0;	//initialize i to check  whether galleria is already started

//start galleria
function startGalleria(){
	//console.log($("#container").height());
	// Initialize Galleria
	if(i == 0){	//if galleria not started
		//console.log('galleria initialized');
		$('#photoGallery').galleria({
			width: 1000,	//photogallery width
			debug: false,			//this is added disable debug messages
			idleMode: false,		//this is added to prevent the disappearence of the nav icons
			 _webkitCursor: false,  //webkit browser used to show nav as cursor which is now disabled
			// thumbCrop: 'width'
			//height: 500
			/*extend: function(options) {
				var gallery = this; // "this" is the gallery instance
				$('#play').click(function() {
	
					gallery.play(); // call the play method
				});
			}*/
		});
		i++;	//count increase if galleria started
		//console.log(i);
	}
}

//get the center position vertically
function get_center_position_vertical(size) {
	size = size.replace(/px/g, '');
	if(size == '' || size == null) {
		alert('Please provide a height value of the popup element to get the center position');
		return false;
	}
	var win_size = get_window_size();
	var win_height = win_size['height'];
	var max_val = Math.max(win_height, size);
	var min_val = Math.min(win_height, size);
	var diff = max_val - min_val;
	var final = Math.round(diff/2);
	return final+'px';
}

//javascript email validation
function validateEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = email;
	if(reg.test(address) == false) {
		//alert('Invalid Email Address, Please enter a valid email address.');
		return false;
	} else {
		return true;
	}
}

//send to friend form error highlighter 
function errorHightlighter(eleId) {
	$('#'+eleId).css({
		'width':'0',
		'height':'14px',
		'background' : '#FFF',
		'color' : '#000',
		'padding' : '5px'
	}).animate({
		'width':'80%'
	});
	$('#mailStatus').hide();
}

//tell a friend icon click in the header
function tellafriendIcon() {
	$('#tellafriendIcon').click(function( event ) {
		//setContainerBgImg('images/backgrounds/plaid.jpg');
		toolTipIpad(event, 'tellafriendIcon');
		removeContainerBgImg();
		setContainerBgImg($('#EmailToFriend_bg').attr('src'));
		
		$('#sendToFriendPopUp').css({
			'display' : 'block',
			'position' : 'relative',
			'left' : get_center_position('450px'),
			//'top' : get_center_position_vertical('260px')
			'top' : ($('#container').height()/2)-(130)
		});	
		$('#photos,#videos,#videoThumbnails,#jp_container_video_1,#photoGallery,#contact,#about,#scrollDown,#scrollUp').css({
			'display':'none'		
		});
	});
}

//hide playlist if visible
function hidePlaylist() {
	//console.log('top='+$('#playlist').css('top'));
	if($('#playlist').css('top') == '-120px') {
		if(!IPHONE && !IPAD && !SAMSUNG) {
			tip.text('OPEN PLAYLIST');
			//tooltipFooter('playlistIcon','OPEN PLAYLIST');	
		} else {
			tip.text('OPEN PLAYLIST');
			//tooltipFooterIpad('playlistIcon','OPEN PLAYLIST');	
		}
		$('#playlist').stop().animate({
				top: 0+'px',
				right: 0,
				opacity:0
			}, 500 , function() {
				$('#playlist').css('visibility','hidden');
			}
		).css({
			'z-index':'-1',
		
		});
		clickCounterPlayList = 0;
	}
}	

function controlDisplayOfAudioBtn() {
	if(manuallyDisabledSound == 0) {
		$('#muteControl').attr('src' ,'images/music_on.png');
		$("#jquery_jplayer_1").jPlayer("play");
	} else if(manuallyDisabledSound == 1) {
		$('#muteControl').attr('src','images/music_off.png');
		$("#jquery_jplayer_1").jPlayer("pause");
	}	
}
