
$().ready(function(){
    $('.job-offers>li').click(function(){
        $(this).siblings('.expanded').removeClass('expanded').find('.job-detail').slideToggle();
        $(this).toggleClass('expanded').find('.job-detail').slideToggle();
    });
    $('#nav a, a.top, #home .stage a, area[href=#contact]').click(slideToHref);
	$('#home .stage').after('<div id="home-pager">');
	$('#home .stage .item-wrapper').cycle({pager:'#home-pager'}).parent().next().find('a').html('&nbsp;');
	$('#home .stage a[href=#apps]').eq(0).click(function(){
		$('#apps-pager a').eq(0).click();
	}).next().click(function(){
		$('#apps-pager a').eq(1).click();
	});
	$('#apps .stage').after('<div id="apps-pager">');
	$('#apps .stage .item-wrapper').css({overflow:'visible'}).cycle({
		//fx:'scrollLeft',
		fx:'fade',
		timeout:0,
		before:function(){
			//console.log($(this).find('img.phone').attr('src'));
			//$(this).find('img.phone').animate({marginLeft:'-250px'},'slow');
			$(this).siblings(':visible').find('img.phone').animate({marginLeft:'-400px'},'fast','swing');
			$(this).find('img.phone').hide().css({marginLeft:'-400px'}).fadeIn().animate({marginLeft:'0px'},'fast','swing');
		},
		pager:'#apps-pager'
	});

	$('div.fileuploader[id]').each(initFileUploader);
	
	$('form').submit(formSubmitHandler);

	initMap();
})

function initFileUploader() {
	new qq.FileUploader({
		element: document.getElementById($(this).attr('id')),
		action: './services/upload/'+$(this).closest('.tile[id]').attr('id'),
		template: '<div class="qq-uploader">' + 
				 '<div class="qq-upload-drop-area"><span>Drop files here to upload</span></div>' + 
				'<div type="button" class="qq-upload-button fileupload"></div>' +
				'<ul class="qq-upload-list"></ul>' + 
			 '</div>',
		debug: true
	});
}

function slideToHref(){
	$(window).scrollTo($(this).attr('href'),'slow');
	if(this.blur)
		this.blur();
	return false;
}

function formSubmitHandler() {
	console.log('formSubmitHandler');
	if($(this).valid()) {
		console.log('form validated, sending using ajaxSubmit');
		$(this).find('input[type=file]').attr('disabled',true);
		$(this).ajaxSubmit( {
			success:	formAjaxHandler,
			dataType:	"json"
		});
		$(this).find('input[type=file]').attr('disabled',false);
	}
	return false;
}

function formAjaxHandler(response) {
	$('#'+response.tile+' form fieldset.controls').html('<p>'+response.msg+'</p>')
}

function initMap() {
	// Find the map div
	var map_container = document.getElementById('map');
	// Find the static map tile and remove it
	var static_map = map_container.getElementsByTagName('img')[0];
	static_map.parentNode.removeChild(static_map);
	// Geocode the address
	
	/*var myIcon = new GIcon(G_DEFAULT_ICON);
	myIcon.image = "frontend/img/contact_map_icon.png";
	myIcon.iconSize = new GSize(30, 29);/**/
    var icon = new google.maps.MarkerImage("frontend/img/contact-map-icon.png");

	var geocoder = new google.maps.Geocoder();
	geocoder.geocode({ 'address': '7 Donegall Square West, Belfast BT1 6, UK'}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			// Set up our map options
			var opts = {
				zoom: 16,
				center: results[0].geometry.location,
				disableDefaultUI: true,
				mapTypeId: google.maps.MapTypeId.ROADMAP			};
			// Create the map
			var map = new google.maps.Map(map_container, opts);
			// Add the marker
			var myLatlng = new google.maps.LatLng(54.596639,-5.931888);
			var marker = new google.maps.Marker({
				position: myLatlng, 
				map: map, 
				icon: icon,
			});
		}
	});
}

if(!window.console) {
	window.console = { log: function(){} };
}


