$(document).ready(function() {
	// Preload all rollovers
	$("#languages img").each(function() {
		// Set the original src
		rollsrc = $(this).attr("src");
		rollON = rollsrc.replace(/.gif$/ig,"_h.gif");
		$("<img>").attr("src", rollON);
	});
	
	// Navigation rollovers
	$("#languages a").mouseover(function(){
		imgsrc = $(this).children("img").attr("src");
		matches = imgsrc.match(/_h/);
		
		// don't do the rollover if state is already ON
		if (!matches) {
			imgsrcON = imgsrc.replace(/.gif$/ig,"_h.gif"); // strip off extension
			$(this).children("img").attr("src", imgsrcON);
		}
		
	});
	$("#languages a").mouseout(function(){
		$(this).children("img").attr("src", imgsrc);
	});
	
	$(".item .mask").css({ "-moz-opacity" : ".75", "filter" : "alpha(opacity=75)", "opacity" : ".75" });
	
	//.parent()
	$(".item").click(function(){
		$.ajax({
			type: "GET",
			url: "ajax-details.php",
			data: "op=get_details&id=" + $(this).attr("id").split("_")[1],
			success: function(response){
				$("#details").html(response);
				$(".socialbookmarking li a").tooltip({ "track": "true", "delay": 0, "showURL": false, "showBody": " - ", "fade": 250 });	
				$('#details').fadeIn("100");
				var position = $(".portfolio-pane").position();
				$("#details").css({"left": position.left});
				$("#details").css({"top": position.top});
				$("#details").css({"width": $(".portfolio-pane").css("width")});
				$("#details").css({"height": $(".portfolio-pane").css("height")});
				$('.details-descriptions').jScrollPane();
			}
		});
	});
	
	$(".item").hover(
		function() { $("#" + this.id + " .mask").stop().animate({"height": "60px"}, 200); },
		function() { $("#" + this.id + " .mask").stop().animate({"height": "18px"}, 200); }
	);
	
	$(".category a").click(function() {
		$(".category").removeClass("selected");
		$(this).parent(".category").addClass("selected");
		thisItem = $(this).attr('rel');
		if(thisItem == "all") {
			$(".item").css({ "display" : "block" });
		}else{
			var nr_items_displayed = 0;
			$(".item > input").parent().css({"display" : "none"});
			for(var i = 0; i < $(".item > input").length; i++){
				if(inArray(thisItem, $(".item > input")[i].value.split(" / "))){
					$(".item > input[value='" + $(".item > input")[i].value + "']").parent().css({"display" : "block" });
				}
			}
		}
		$(".portfolio-pane").jScrollPane();
		$("#details").fadeOut("fast");
	});
	
	$(".services-list a").mouseover(function(){ switch_tabs($(this)); });
	
	switch_tabs($(".defaulttab"));
	
	var originalFontSize = $(".minh").css("font-size");
	$(".resetFont").click(function(){ $(".minh").css("font-size", originalFontSize); $(".minh").jScrollPane(); });
	
	// Increase Font Size
	$(".increaseFont").click(function(){ increaseFont($(".minh"), 13); $(".minh").jScrollPane(); });
	
	// Decrease Font Size
	$(".decreaseFont").click(function(){ decreaseFont($(".minh"), 11); $(".minh").jScrollPane(); });
	
	$("#ajax-contact-form input[type=text],textarea").each(function(){
		var default_value = this.value;
		$(this).blur(function(){
			if($(this).val() == ""){
				$(this).val(default_value);
			}
		});
		$(this).focus(function(){
			if($(this).val() == default_value){
				$(this).val("");
			}
		});
	});
	
	$("#ajax-contact-form").submit(function(){
		var str = $(this).serialize();
		$.ajax({
			type: "POST",
			url: "ajax-validate.php",
			data: str,
			success: function(msg){
				$("#note").css({"display" : "block"});
				if(jQuery.parseJSON(msg).success){
					$("#note").html(jQuery.parseJSON(msg).success);
					$("#note").delay(3000).slideUp(function(){ $("#note").css({"display" : "none"}); });
				}
				if(jQuery.parseJSON(msg).error){
					$("#note").html(jQuery.parseJSON(msg).error);
				}
				reloadCaptcha();
			}
		});
		return false;
	});
	
	/*$("*", document).click(function (e) {
		e.stopPropagation();
		var domEl = $(this).get(0);
		//alert(domEl.id)
		if(domEl.id != "details"){ $("#details").fadeOut("fast"); }
	});*/
});

function inArray(needle, haystack){
	for(var i in haystack){
		if(needle == haystack[i]){
			return true;
		}
	}
	return false;
}

function switch_tabs(obj){
	$('.servbox').hide();
	$('.services-list a').removeClass("selected");
	var id = obj.attr("rel");
	
	$(id).show();
	obj.addClass("selected");
	$('.minh').jScrollPane();
}

function decreaseFont(container, min_size){ 
	var currentFontSizeNum = parseFloat($('.minh').css('font-size'), 10);
	if(currentFontSizeNum > min_size){
		container.css('font-size', (currentFontSizeNum - 1) + "px");
	}
}

function increaseFont(container, max_size){
	var currentFontSizeNum = parseFloat($('.minh').css('font-size'), 10);
	if(currentFontSizeNum < max_size){
		container.css('font-size', (currentFontSizeNum + 1) + "px");
	}
}

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;
	}
}

/*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * Licensed under the MIT License (LICENSE.txt).
 *
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 * Thanks to: Seamus Leahy for adding deltaX and deltaY
 *
 * Version: 3.0.4
 * 
 * Requires: 1.2.2+
 */

var types = ['DOMMouseScroll', 'mousewheel'];

$.event.special.mousewheel = {
    setup: function() {
        if ( this.addEventListener ) {
            for ( var i=types.length; i; ) {
                this.addEventListener( types[--i], handler, false );
            }
        } else {
            this.onmousewheel = handler;
        }
    },
    
    teardown: function() {
        if ( this.removeEventListener ) {
            for ( var i=types.length; i; ) {
                this.removeEventListener( types[--i], handler, false );
            }
        } else {
            this.onmousewheel = null;
        }
    }
};

$.fn.extend({
    mousewheel: function(fn) {
        return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
    },
    
    unmousewheel: function(fn) {
        return this.unbind("mousewheel", fn);
    }
});


function handler(event) {
    var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
    event = $.event.fix(orgEvent);
    event.type = "mousewheel";
    
    // Old school scrollwheel delta
    if ( event.wheelDelta ) { delta = event.wheelDelta/120; }
    if ( event.detail     ) { delta = -event.detail/3; }
    
    // New school multidimensional scroll (touchpads) deltas
    deltaY = delta;
    
    // Gecko
    if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
        deltaY = 0;
        deltaX = -1*delta;
    }
    
    // Webkit
    if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
    if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
    
    // Add event and delta to the front of the arguments
    args.unshift(event, delta, deltaX, deltaY);
    
    return $.event.handle.apply(this, args);
}
