﻿var isIE = false;
var movingCase = false;

$.fn.tagName = function() {
    return this.get(0).tagName;
}


$(document).ready(function(){
	isIE = $.browser.msie;
	$(document).pngFix(); 
		
	if( $('form[name=contactForm]').length>0 ){
		var value1 = $('#cName').val();
		var value2 = $('#cEmail').val();
		var value3 = $('#cComments').val();
		$('#cName')
			.focus(function(){$(this).addClass('active'); if($(this).val()==value1){$(this).val('');}})
			.blur(function(){$(this).removeClass('active'); if($(this).val()==''){$(this).val(value1);}});
			var value = $('input').val();
		$('#cEmail')
			.focus(function(){$(this).addClass('active'); if($(this).val()==value2){$(this).val('');}})
			.blur(function(){$(this).removeClass('active'); if($(this).val()==''){$(this).val(value2);}});
			var value = $('input').val();
		$('#cComments')
			.focus(function(){$(this).addClass('active'); if($(this).val()==value3){$(this).val('');}})
			.blur(function(){$(this).removeClass('active'); if($(this).val()==''){$(this).val(value3);}});
	}
	if( $('form[name=remindForm]').length>0 ){
		var value2 = $('#rEmail').val();
		$('#rEmail')
			.focus(function(){$(this).addClass('active'); if($(this).val()==value2){$(this).val('');}})
			.blur(function(){$(this).removeClass('active'); if($(this).val()==''){$(this).val(value2);}});
			var value = $('input').val();
	}
	if( $('#lEmail').length>0 ){
		var lEmail = $('#lEmail').val();
		var lPassword = $('#lPassword').val();
		$('#lEmail')
			.focus(function(){if($(this).val()==lEmail){$(this).val('');}})
			.blur(function(){if($(this).val()==''){$(this).val(lEmail);}});
		$('#lPassword')
			.focus(function(){if($(this).val()==lPassword){$(this).val('');}})
			.blur(function(){if($(this).val()==''){$(this).val(lPassword);}});
	}
	if( $('#lEmail').length>0 ){
		var lEmail = $('#lEmail').val();
		var lPassword = $('#lPassword').val();
		$('#lEmail')
			.focus(function(){if($(this).val()==lEmail){$(this).val('');}})
			.blur(function(){if($(this).val()==''){$(this).val(lEmail);}});
		$('#lPassword')
			.focus(function(){if($(this).val()==lPassword){$(this).val('');}})
			.blur(function(){if($(this).val()==''){$(this).val(lPassword);}});
	}
	
	
	$('#cName,#cEmail').keydown(function (e) {
		if (e.which != 13){
			$('#formError').fadeOut(300);
		} else {
			$('#cSubmit').click();	
		}
	});
	$('#rEmail').keydown(function (e) {
		if (e.which != 13){
			$('#formError').fadeOut(300);
		} else {
			$('#rSubmit').click();	
		}
	});
	$('#cComments').keydown(function (e) {if (e.which != 13)$('#formError').fadeOut(300);});
	$('#cReset').click(function(){
		document.contactForm.reset();
	});
	$('#cSubmit').click(function(){
		//alert("asdf");
		val1 = $.trim($('#cName').val());
		val2 = $.trim($('#cEmail').val());
		val3 = $.trim($('#cComments').val());
		$('#formError').html('');
		$('#formLoad').show();
		postData = {module:'contact',method:'sendEmail',cComments:escape(val3),cEmail:val2,cName:escape(val1)}
		$.post('addons/ajaxProxy.asp',postData,function(data){
			$('#formLoad').hide();
			if(result){
				$('#contactForm').animate({ opacity: 0, height: 'toggle' }, 500, function(){
					$('#contactForm').css({'font-weight':'bold', 'color':'#afd136'});
					$('#contactForm').html(message);	
					$('#contactForm').animate({ opacity: 100, height: 'toggle' }, 300);
				});
			}else{
				$('#formError').html(errorMessage);
				$('#formError').fadeIn(300);
			}
		},'script');
	});
	
	$('#rReset').click(function(){
		document.remindForm.reset();
	});
	$('#rSubmit').click(function(){
		val2 = $.trim($('#rEmail').val());
		$('#formError').html('');
		$('#formLoad').show();
		postData = {module:'remindPassword',method:'sendEmail',rEmail:val2}
		$.post('addons/ajaxProxy.asp',postData,function(data){
			$('#formLoad').hide();
			if(result){
				$('#remindForm').animate({ opacity: 0, height: 'toggle' }, 500, function(){
					$('#remindForm').css({'font-weight':'bold', 'color':'#afd136'});
					$('#remindForm').html(message);	
					$('#remindForm').animate({ opacity: 100, height: 'toggle' }, 300);
				});
			}else{
				$('#formError').html(errorMessage);
				$('#formError').fadeIn(300);
			}
		},'script');
	});
	
	
	$('#loginButton').click(function(){
		$.get('addons/ajaxProxy.asp',{module:'header',method:'loginUser',email:$('#lEmail').val(),la:$('#hla').val(), pass:$('#lPassword').val()},function (data) {
			$('#temp').html(data);
			if(validlogin){
				$('#temp').html('');
				$('#topLogin').html(data);
			}else{
				jAlert(hloginErrorMessage, 'Login Error');
			}
		},'html');
	});
	$('#lEmail').keydown(function (e) {
		if (e.which == 13) $('#loginButton').click();
	});
	$('#lPassword').keydown(function (e) {
		if (e.which == 13)	$('#loginButton').click();
	});	
	
	if( $('#newsletterText').length>0 ){
		var newsletterText = $('#newsletterText').val();
		$('#newsletterText')
			.focus(function(){if($(this).val()==newsletterText){$(this).val('');}})
			.blur(function(){if($(this).val()==''){$(this).val(newsletterText);}})
			.keydown(function (e){if (e.which == 13) $('#newsletterButton').click();})
			.keypress(function (e){if (e.which != 13) $('#msgText').fadeOut(300);});
	
		$('#newsletterButton').click(function () {
			var mail=$.trim($('#newsletterText').val());
			$('#nwltr_indicator').toggle();
			$.post('addons/ajaxProxy.asp',{module:'Footer',method:'newsletterSubscribe',inject:'var subcribeResult=false\; var subcribeResultText=\'Please fill the field with a valid email address\'',email:$('#newsletterText').val()},function (data) {
				$('#nwltr_indicator').toggle();
				if(subcribeResult){
					$('#newsletterText').blur();
					$('#msgText').html(subcribeResultText);
					$('#msgText').css('color','#afd136');
					$('#msgText').fadeIn(300);
					$('#newsletterText').val(newsletterText);
				}else{
					$('#msgText').html(subcribeResultText);
					$('#msgText').css('color','#cd2d2d');
					$('#msgText').fadeIn(300);
				}
   			},'script');
    	});
	}
	
	$('#nextCase').click(function(){
		var w=$('#clCarousel').width();
		var l=parseInt($('#clCarousel').css('left').replace('px',''));
		var step = 284;
		if(!movingCase && (w+l)>step ) var t=l-step; else if(!movingCase && (w+l)==step ) var t=0;
		movingCase=true; 
		$('#clCarousel').animate({'opacity':0},300,function(){$(this).css({'left':t+'px'});$(this).animate({'opacity':1},300); movingCase=false; })
	});
	$('#prevCase').click(function(){
		var w=$('#clCarousel').width();
		var l=parseInt($('#clCarousel').css('left').replace('px',''));
		var step = 284;
		if(!movingCase && l<0 ) var t=l+step; else if(!movingCase && l==0 ) var t=step-w;
		movingCase=true; 
		$('#clCarousel').animate({'opacity':0},300,function(){$(this).css({'left':t+'px'});$(this).animate({'opacity':1},300); movingCase=false; })
	});
	
	if($('#contactMap').length>0) mapInit();
});

function showReel(){
	reelActive=true;
	$('#reel').stop(true, true).find('.workWrapper').css("opacity", 0);
	var scrollPoint = calcScroll('reelImage','');
	$.scrollTo(scrollPoint,200,function(){
		$('#reel').slideDown(300,function(){
			$(this).find('.description').jScrollPane({showArrows:true, scrollbarWidth: 10, arrowSize: 10});
			$(this).find('.cheet').hide();
		}).find('.workWrapper').fadeTo('slow', 1);
	});
}

function hideReel(){
	$('#reel').stop(true, true).find('.workWrapper').animate({"opacity": 0});	
	$('#reel').slideUp(300);
	reelActive=false;
}

function getWorks(id){
	if(id!=activeFilter){
		$('#flt_'+activeFilter).removeClass('active');
		$('#flt_'+id).addClass('active');
		activeFilter = id;
	}
}

$.fn.toggleWork = function() {
	var wid = $(this).attr('id');
	var itmla = $(this).attr('rel');
	var itmid = getItmID(itmla);
	var langid = getLangID(itmla);
	//console.log('activeWID: '+activeWID);
	//console.log('wid: '+wid);
	if(wid!=activeWID){
		$('#'+wid).addClass('waiting');
		httpRequest = true;
		var tR = getShowRow(wid);			//target row
		var cR = getShowRow(activeWID); 	//opened row
		var scrollRow = getRow(wid);
		var ind = getIndex(wid);		//itm column
		var scrollPoint = calcScroll(scrollRow,cR);
		//make requested work active and inactivate
		
		//console.log('opened row: '+cR);
		//console.log('target row: '+tR);
		
		if(cR!=''){
			if(cR==tR){
				$.scrollTo(scrollPoint,200,function(){
					$('#'+cR).stop(true, true).find('.workWrapper').animate({"opacity": 0});	
					$('#'+cR).slideUp(300,function(){
						$('#'+cR).html('');
						$('#'+cR).showRow(wid,itmid,langid,ind)
					});	
				});
			}else if(cR!=tR){
				$.scrollTo(scrollPoint,200,function(){
					$('#'+cR).stop(true, true).find('.workWrapper').animate({"opacity": 0});	
					$('#'+cR).slideUp(300,function(){
						$('#'+tR).showRow(wid,itmid,langid,ind)
						$(this).html('');
					});
				});	
			}
		}else{
			$.scrollTo(scrollPoint,200,function(){
				$('#'+tR).showRow(wid,itmid,langid,ind);
			});	
		}
	} else {
		httpRequest = true;
		var cR = getShowRow(activeWID); 	//opened row
		var scrollRow = getRow(wid);
		var scrollPoint = calcScroll(scrollRow,cR);
		$('#'+activeWID).removeClass('active');
		$.scrollTo(scrollPoint,200,function(){
			$('#'+cR).stop(true, true).find('.workWrapper').animate({"opacity": 0});	
			$('#'+cR).slideUp(300,function(){
				httpRequest = false;
				activeWID='';
				$(this).html('');
			});
		});
	}
};


$.fn.showRow = function(id1,itm,la,ind) {
	var $this = $(this)
	//console.log($this);
	//var params = 'module=works&method=getWork&itm=' + id1;
	postData = {module:'works',method:'getWork',itm:itm,la:la,ind:ind}
	$.get('addons/ajaxProxy.asp',postData,function(data){
		httpRequest = false;
		if (activeWID!='') $('#'+activeWID).removeClass('active');
		$('#'+id1).removeClass('waiting');
		$('#'+id1).addClass('active');
		activeWID = id1;
		$this.html(data);
		$this.stop(true, true).find('.workWrapper').css("opacity", 0);
		$this.slideDown(300,function(){
			$this.find('.description').jScrollPane({showArrows:true, scrollbarWidth: 10, arrowSize: 10});
			$this.find('.cheet').hide();
		}).find('.workWrapper').fadeTo('slow', 1);
	});
}

function getShowRow(wid){
	if(wid!='')	return wid.substring(0,wid.lastIndexOf('_')).replace('item_','workShow_');
	return '';
}
function getRow(wid){
	if(wid!='')	return wid.substring(0,wid.lastIndexOf('_')).replace('item_','row_');
	return '';
}
function getIndex(wid){
	if(wid!='')	return wid.substring(wid.lastIndexOf('_')+1);
	return '';
}
function getItmID(str){
	if(str!='')	return str.split('_')[0];
	return '0';
}
function getLangID(str){
	if(str!='')	return str.split('_')[1];
	return '0';
}
function closeWork(){
	$('#'+activeWID).toggleWork();
}
	
function calcScroll(r,c){
	var doc = $(document).height(); 							//full document height
	if(c!='') var work = $('#'+c).height(); else var work=0;	//current opened work height
	var scroll = $('#'+r).position().top;						//position where I want to scroll is the start of the target row
	var win = $(window).height();								//current window view height
	var docClose = doc-work;									//document height when the opened work will close
	var bottomDistance = docClose-scroll;						//distance of the point I want to scroll from the bottom of the document
	if(bottomDistance<win)										//if the bottom distance is less than the current window view I can't scroll to my target
		var scr = docClose-win-10;									//So I will scroll to the point that the bottom of the document meets the bottom of the window
	else
		if(r=='reelImage')
			var scr = scroll-99;									//else I'll just scroll where I want.
		else
			var scr = scroll-50;
	scr=scr<0?0:scr;
	return scr;
}

function mapInit() {
	var baseIcon = new GIcon();
	baseIcon.shadow = 'template/images/mapShadow.png';
	baseIcon.image = 'template/images/mapIcon.png';
	baseIcon.iconSize = new GSize(79, 47);
	baseIcon.shadowSize = new GSize(94, 47);
	baseIcon.iconAnchor = new GPoint(29, 47);
	baseIcon.shadowAnchor = new GPoint(29, 47);
	
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById('contactMap'));
		map.addMapType(G_HYBRID_MAP); 
		map.enableScrollWheelZoom(); 
		var minMapScale = 5; 
		map.addControl(new GSmallZoomControl3D()); 
		bounds = new GLatLngBounds();
		var mapTypes = map.getMapTypes();
		for (var i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMinimumResolution = function() {return minMapScale;}
		}
	}  


	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById('contactMap'));
		map.addMapType(G_HYBRID_MAP);
		map.enableScrollWheelZoom();
		map.addControl(new GSmallZoomControl3D()); 
		
		var point = new GLatLng(37.922942, 23.7057566);
		var markerIcon = new GIcon(baseIcon);
		markerOptions = { icon: markerIcon };
		var marker = new GMarker(point); //, markerOptions);
		map.addOverlay(marker);
		map.setCenter(point, 15);
		var minMapScale = 5; 
		var mapTypes = map.getMapTypes();
		for (var i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMinimumResolution = function() {return minMapScale;}
		}
	} 
}
