var _sAJAXFile = '/include/aj.php';
var _sDelimiter = '||:|:||';

$(document).ready( function(){
			 			
	if( $('#portfolio').length == 1)
		_xPortfolio.init();
	
	if( $('#contact').length == 1)
		_xContact.init();
	
});


_xPortfolio = {

	init : function(){
		 
		$('#portfolio .col-1 ul li a').click( function(){
			var l_nId = $(this).attr('rel');
			var l_sURL = $(this).attr('href');
			$('#portfolio .col-1 ul li a').removeClass('itemlinkactive');
			$(this).addClass('itemlinkactive');
			_xPortfolio.getProject(l_nId);
			_xTracker.trackPage(l_sURL);
			return false;
		});
		
		
		$('#portfolio .col-1 h3 a').click( function(){
			var l_sId = $(this).attr('rel');
			var l_sURL = $(this).attr('href');
			_xPortfolio.showHideYear(l_sId);
			_xTracker.trackPage(l_sURL);
			return false;
		});
		
		$('.secondary').click( function(){
			var l_sURL = $(this).attr('href');
			var l_sClass = $(this).attr('class');
			$.scrollTo('.additional-links',{ duration:500});
			_xTracker.trackPage(l_sURL+l_sClass);
			return false;
		});
		  
		
	},
		
	showHideYear : function(p_sYear){
		 
		$('#portfolio .col-1 h3 a').each( function(){
			if( $(this).attr('rel') == p_sYear)
				$(this).addClass('linkactive').attr('disabled','disabled');
			else
				$(this).removeClass('linkactive').attr('disabled','');
		});
		
		$('#portfolio .col-1 ul').each( function(){										 
			if( $(this).attr('id') != 'year-' + p_sYear)
				$(this).hide("fast");
			else
				$(this).show("fast");
			
		});
	},
	
	getProject : function(p_nId){
		
		 $.ajax({
			type: "GET",
			url: _sAJAXFile,
			data: "a=vp&p_nId="+p_nId,
			success: function(msg){
				eval(msg);
			}
		 });
		
	}

}

_xTracker = {
	
	_bPerformTrack : false,
	trackPage : function(p_sPage){
		
		if(this._bPerformTrack)
			pageTracker._trackPageview(p_sPage); 
			//alert(p_sPage);
	}
}


_xContact = {

	_bCanSubmit : true,
	_aSendingVars: '',
	init : function(){
		
		$('#contact #form-contact').submit( function(){
			_xContact.submitForm();
			return false;
		});
		
		$('#contact #form-contact input,#contact #form-contact select,#contact #form-contact textarea').each( function(){
			$(this).focus( function(){ 	$(this).addClass('focus');});
			$(this).blur( function(){ 	$(this).removeClass('focus');});
		});
		
		$('#contact #form-contact select').change( function(){ 
				_xContact.howHearChange();				  
		});
		
		$('#contact #form-contact #otherhear').each( function(){
			$(this).val($(this).attr('alt'));
			$(this).focus( function(){ 
				if( $(this).val() == $(this).attr('alt') ){
					$(this).val('');
				}
			});
		});
		  
		_xContact.howHearChange();
		
		 
	},
	
	killfocus : function(){
		$('#contact #form-contact input,#contact #form-contact select,#contact #form-contact textarea').each( function(){
			$(this).blur();
		});
	},
	
	submitForm : function(){
		this._bCanSubmit= true;
		this.killfocus();
		$('#contact #form-contact input,#contact #form-contact select,#contact #form-contact textarea').each( function(){
		 	if( l_nAccessKey = $(this).attr('accesskey') || $(this).attr('title')  ){
				if(l_nAccessKey == 1 || $(this).attr('title') ){
					if(_xUtils.checkEmpty($(this).val())){
						alert('Please enter ' + $(this).attr('title'));	
						$(this).focus();
						_xContact._bCanSubmit = false;
						return false;
					}
				}else if(l_nAccessKey == 2){
					if(!_xUtils.checkEmail($(this).val())){
						alert('Please enter ' + $(this).attr('title'));	
						$(this).focus();
						_xContact._bCanSubmit = false;
						return false;
					}
				}
				
			}
		 });
		
		if(this._bCanSubmit){
			
			_xContact._aSendingVars = new Array();
		
			$('#contact #form-contact input,#contact #form-contact select,#contact #form-contact textarea').each( function(){
				_xContact._aSendingVars.push( Base64.encode( $(this).attr('id') + _sDelimiter + $(this).val()));				
			});
		    
			$.ajax({
				type: "POST",
				url: _sAJAXFile,
				data: "a=cf&args="+_xContact._aSendingVars.join(','),
				success: function(msg){
					eval(msg);
				}
			 });
		}
		
		
		
	},
	
	howHearChange : function(){
		var l_sVal = $('#contact #form-contact select').val();
		if( l_sVal == 'Friend' || l_sVal == 'Other'){
			$('#otherhear').val($('#otherhear').attr('alt')).show("fast");
		}else
			$('#otherhear').hide("fast");
		
	}
}


_xUtils = {
 
  	checkEmpty : function(p_sVal){
		l_sVal = escape(p_sVal);
		l_sVal = l_sVal.replace(/\s*((\S+\s*)*)/, "$1");
		l_sVal = l_sVal.replace(/((\s*\S+)*)\s*/, "$1");
		return l_sVal.length < 1;
	}
	,
	checkEmail : function(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){ return false }
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ return false }
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false }
		if (str.indexOf(at,(lat+1))!=-1){ return false }
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ return false }
		if (str.indexOf(dot,(lat+2))==-1){ return false }
		if (str.indexOf(" ")!=-1){ return false }
		return true					
	},
	checkAlphaNumeric : function(p_sVal){
		return !(/[^a-zA-Z0-9]/.test(p_sVal));	
	}
}



var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
