/*
*	drop down navbar
*/
$(document).ready(function(){
	$("#navbar > li").hover(function(){
		$(this).addClass("hover").children(".submenu").show();
	},function(){
		$(this).removeClass("hover").children(".submenu").hide();
	});
});



/*
*	fire ze jQueryUI cannons
*/
$(document).ready(function(){
	$(".contentslider").tabs({fx: {opacity: 'toggle', duration: 'fast'}}).tabs("rotate", 8000);
	$("#tabs-services").tabs({fx: {opacity: 'toggle', duration: 'fast'}});
	$("#tabs-process").tabs();
});

/*
*	Show tahnkyou message
*/
$(document).ready(function(){
	if ($(".msg").length > 0)
		$(".msg").fadeIn('slow');
});

/*
*	portfolioplant
*/
$(document).ready(function(){
	var opened = false;
	var h = $("#showcase").height();
	
	$("#showcase").css({height:'100px', overflow:'hidden'});

	$("#portfolioplant .readmore-link").click(function(e){
		e.preventDefault();
		
		if (opened) {	
			$(this).html('<p class="readmore-link"><a class="button" href="#" title="Show more">Show more &raquo;</a></p>');
			$("#showcase").animate({"height": "100px"}, {queue:false, duration:1500, easing: 'easeOutQuint'});	
		} else {
			$(this).html('<p class="readmore-link"><a class="button" href="#" title="Hide">Hide &raquo;</a></p>');
			$("#showcase").animate({"height": h}, "600");
		}
			
		opened = !opened;
	});
});

/* Portfolio Tooltip 
*
*/
$(document).ready(function(){
	var x = -20;
	var y = -50;

	$(".row li .bubble").each(function(){
		if (x == 140 * 4 - 20)
		{
			x = -20;
			y = y + 105;
		}
		$(this).css({ left: x + 'px', top:y + 'px'});
		x = x + 140;
	});

	$(".row li").hover(
		function(){
			$(this).find('.bubble').stop(true, true).animate({opacity: 'toggle'}, {queue:false, duration:400});
		},
		function(){			
			$(this).find('.bubble').stop(true, true).animate({opacity: 'toggle'}, {queue:false, duration:400});
		}
	);
});

/* Portfolio-details Tooltip 
*
*/
$(document).ready(function(){
	var x = -20;
	$(".details li .bubble").each(function(){
		$(this).css({ left: x + 'px', top:'-60px'});
		x = x + 146;
	});
	$(".details li").hover(
		function(){
			$(this).find('.bubble').stop(true, true).animate({opacity: 'toggle'}, {queue:false, duration:400});
		},
		function(){			
			$(this).find('.bubble').stop(true, true).animate({opacity: 'toggle'}, {queue:false, duration:400});
		}
	);
});

/*
*	Google Map
*/
$(document).ready(function(){
	if ($("#map").length > 0)
		initialize();
		
	function initialize() {
		var myLatlng = new google.maps.LatLng(1.310349,103.837259);

		var stylez = [
		{
		  featureType: "road",
		  elementType: "all",
		  stylers: [
			{ hue: "#22aaff" },
			{ saturation: 0 }
		  ]
		},

		{
		  featureType: "landscape",
		  elementType: "geometry",
		  stylers: [
			{ hue: "#22aaff" }
		  ]
		}
		];
		  
		var myOptions = {
			zoom: 16,
			center: myLatlng,
			mapTypeControl: false,
			mapTypeControlOptions: { mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'Comwerks'] }
		}
		
		var styledMapOptions = { name: "Comwerks" }
		var MapType = new google.maps.StyledMapType(stylez, styledMapOptions);
		
		var map = new google.maps.Map(document.getElementById("map"), myOptions);
		map.mapTypes.set('Comwerks', MapType);
		map.setMapTypeId('Comwerks');

		var beachMarker = new google.maps.Marker({
			position: myLatlng,
			map: map,
			icon: 'http://www.comwerks.com/images/flag.png',
			title:"We are here!"
		});	
	}	
});




/*
*	
*/
$(document).ready(function(){
	// fire Super Team grouping effect
	$("#teamcontrol").group();

	// for Lego block
	$('#quoteUpdate').rate({output:'outputUpdate', img_off:'/css/images/block_off.png', img_on:'/css/images/blue_block.png'});
	$('#quoteBuzz').rate({output:'outputBuzz', img_off:'/css/images/block_off.png', img_on:'/css/images/yellow_block.png'});
	$('#quoteMakeover').rate({output:'outputMakeover', img_off:'/css/images/block_off.png', img_on:'/css/images/red_block.png'});
	$('#quoteContent').rate({output:'outputContent', img_off:'/css/images/block_off.png', img_on:'/css/images/blue_block.png'});
	$('#quotePassion').rate({output:'outputPassion', img_off:'/css/images/block_off.png', img_on:'/css/images/yellow_block.png'});
	$('#quoteHelp').rate({output:'outputHelp', img_off:'/css/images/block_off.png', img_on:'/css/images/red_block.png'});
	
	// cache all requried field
	var required = $("input[type='text'].required").add("textarea.required");
	var is_email = $("input[type='text'].isEmail");
	var all_fields = $("input[type='text']").add("textarea");
	
	// Set all label into input box
	$("input[type='text']").add("textarea").each(function(){
		$(this).val($(this).prev().text());
	});
	
	// Set required field
	required.each(function(){
		$(this).parent().append('<span class="req">*</span>');
	});
		
	// for focus and blur on input boxes
	all_fields.blur(function(){
		if ($(this).val() == "")
			$(this).val($(this).prev().html());
	});
	
	all_fields.focus(function(){
		$(this).removeClass('error');
		if ($(this).val() == $(this).prev().html())
			$(this).val("");
	});	
	
	// Validate Quotation form
	$("form").submit(function() {	
		// Remove found error(s) if there is/are previous error(s)
		$(this).find('.error').remove();
		$(this).find('.error_bg').removeClass('error_bg');
		
		var error = false;
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		
		// detect if required fields are empty or same as label
		required.each(function() {
			if ($(this).val() == $(this).prev().text() || $(this).val() == '') {
				error = true;
				$(this).addClass('error_bg');
				$(this).parent().append('<div class="error">' + $(this).prev().text() + ' is a required field.</div>');
			}		
		});

		// this is for checking email pattern when the filed is not a compulsory field
		is_email.each(function() {
			if ($(this).val() != $(this).prev().text() && $(this).val() !== '')
			{
				if (!(emailPattern.test($(this).val()))) { 
					error = true;
					$(this).addClass('error_bg');
					$(this).parent().append('<div class="error">Please enter a valid email.</div>');
				}
			}
		});
		
		if (error) {
			return false;
		}
	});	
});


/*
 * Show/collapse the Tear
 */
$(document).ready(function(){
       $('#tearintheuniverse-open').mouseenter(function(){
               $(this).effect("shake", { times:3, distance:2 }, 100)
       }).click(function(){
               $(this).stop(true, true).fadeOut('fast', function(){
                       $('#tearintheuniverse').fadeIn();
                       $('#tearintheuniverse-close').show();
                       $('#tearintheuniverse-spacer').animate({ height: '120px' }, 'fast');
               });
       });
       $('#tearintheuniverse-close').click(function(){
                       $('#tearintheuniverse').fadeOut();
                       $('#tearintheuniverse-close').hide();
                       $('#tearintheuniverse-spacer').animate({ height: '40px' }, 'fast');
                       $('#tearintheuniverse-open').fadeIn('fast');
       });
});
