
jQuery(document).ready(function() {
	
	var $containers = jQuery('div.mse-box');
	$containers.hide();
	
	jQuery('#searchsubmit').click(function() {
		var value = jQuery('#searcharea').val();
		
		if (value.match(/skill/i)) {
			
			jQuery('#resultjobinside').show().find('.mse-boxbody').html('Inget resultat på Jobinside hittades');
			jQuery('#resultweb').show().find('.mse-boxbody').html('<a href="http://www.skill.se" target="_blank">http://www.skill.se</a><br><br><a href="http://ungabyrakrater.skill.se" target="_blank">http://ungabyrakrater.skill.se</a><br><br><a href="http://it-talang.skill.se" target="_blank">http://it-talang.skill.se</a><br><br><a href="http://www.facebook.com/skilligt" target="_blank">http://www.facebook.com/skilligt</a><br><br><a href="http://www.affarsliv.com/brighteast/" target="_blank">http://www.affarsliv.com/brighteast/</a><br><br><a href="http://talentbranding.wordpress.com/" target="_blank">http://talentbranding.wordpress.com/</a><br><br><a href="http://www.hrsverige.nu/profile/LenaMiranda" target="_blank">http://www.hrsverige.nu/profile/LenaMiranda</a><br><br>');
			jQuery('#resultblog').show().find('.mse-boxbody').html('<a href="http://talentbranding.wordpress.com/" target="_blank">Talent Branding</a><br>Inspiration för att skapa attraktiva arbetsgivarvarumärken');
			
			jQuery('#resultimage').show().find('.mse-boxbody').html('<img height="100px" src="http://skill.se/wp-content/blogs.dir/2/files/2010/09/76-kvinnliga-kollegor-siffror.png" /> <img height="100px" src="http://media02.linkedin.com/media/p/2/000/01b/3dc/1709ec5.png" /> <img height="100px" src="http://skill.se/wp-content/uploads/2011/02/47781_430121961931_172492551931_5694377_5495678_n-700x451.jpg" /> <img height="100px" src="http://it-talang2010.skill.se/files/2010/09/skill_bilder_03-05.jpg" /> <img height="100px" src="http://t1.gstatic.com/images?q=tbn:ANd9GcS5u8cDhwz-2YkIZPDESFCMohbr2tps6VSSxk0JIlKbgnRbo-hICQ&t=1" />');
			
			jQuery('#resultvideo').show().find('.mse-boxbody').html('<iframe width="250" height="200" src="http://www.youtube.com/v/IpaSE7E6rV0&fs=1&source=uds&autoplay=0" frameborder="0" allowfullscreen></iframe><br><br><iframe width="250" height="200" src="http://www.youtube.com/v/o0XgI1QBOoU&fs=1&source=uds&autoplay=0" frameborder="0" allowfullscreen></iframe><br><br><iframe width="250" height="200" src="http://www.youtube.com/v/xVSUgQKjJx8&fs=1&source=uds&autoplay=0" frameborder="0" allowfullscreen></iframe><br><br><iframe src="http://player.vimeo.com/video/21057357?title=0&amp;byline=0&amp;portrait=0" width="250" height="200" frameborder="0"></iframe><br><br><iframe src="http://player.vimeo.com/video/16444985?title=0&amp;byline=0&amp;portrait=0" width="250" height="200" frameborder="0"></iframe><br><br><iframe src="http://player.vimeo.com/video/23019612?title=0&amp;byline=0&amp;portrait=0" width="250" height="200" frameborder="0"></iframe>');
			jQuery('#resultmaps').show().find('.mse-boxbody').html('<img src="http://maps.google.com/maps/api/staticmap?center=stockholm&zoom=5&size=250x200&maptype=roadmap&sensor=false&markers=color:blue%7Clabel:S%7C58.412065,15.6267" />');
			jQuery('#resultwiki').show().find('.mse-boxbody').html('Inget resultat på Wikipedia hittades');
			jQuery('#resultfb_post').show().find('.mse-boxbody').html('Inga öppna facebookposter hittades');
			jQuery('#resultfb_group').show().find('.mse-boxbody').html('<a href="http://www.facebook.com/skilligt" target="_blank">Skilligt</a>');
			
		} else {
			
		$containers.find('.mse-boxbody').html('<img src="ajax-loader.gif" />');
		$containers.hide();
		
		//Set search
		jQuery.ajax({ 
			url: '/wp-content/themes/skill/mse-search.php?searchtype=searchword&q='+value, 
			type: 'GET',
			dataType: 'text',
			timeout: 10000,
			success: function(rsp) {
				//console.log('logging complete', rsp);
			},
			error: function(XMLHttpRequest, textstatus) {

				if (textstatus == 'timeout') {

				} else {

				}
			},
			complete: function() {
			}
		});		
		
		//Web - Jobinside
		if (jQuery('#checkbox-jobinside').is(':checked')) {

			var $resultcontainer = jQuery('#resultjobinside');
			$resultcontainer.show();

			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=googlesearch&v=1.0&h1=sv&rsz=8&q="'+value+'" site:jobinside.se', 
				type: 'GET',
				dataType: 'json',
				timeout: 10000,
				success: function(rsp) {
					
					var $resultcontainer = jQuery('#resultjobinside .mse-boxbody');
					var count = 0;
					$resultcontainer.html('');
					jQuery.each(rsp.responseData.results, function(key, node) {
						count++;
						jQuery('<a></a>').attr('href', unescape(node.url)).html(node.title).appendTo($resultcontainer);
						$resultcontainer.append('<br /><br />');
					});
					if (count == 0) {
						$resultcontainer.append('Ingen info på Jobinside hittades');
					}
				},
				error: function(XMLHttpRequest, textstatus) {
					var $resultcontainer = jQuery('#resultjobinside .mse-boxbody');
					$resultcontainer.html('');
					if (textstatus == 'timeout') {
						$resultcontainer.append('Ingen info på Jobinside hittades');
					} else {
						$resultcontainer.append('Ingen info på Jobinside hittades');
					}
				},
				complete: function() {
				}
			});
			
		}
			
		if (jQuery('#checkbox-web').is(':checked')) {
			
			var $resultcontainer = jQuery('#resultweb');
			$resultcontainer.show();
			
			//Web
			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=googlesearch&v=1.0&h1=sv&rsz=8&q="'+value+'"', 
				type: 'GET',
				dataType: 'json',
				timeout: 10000,
				success: function(rsp) {
					var $resultcontainer = jQuery('#resultweb .mse-boxbody');
					var count = 0;
					$resultcontainer.html('');
					jQuery.each(rsp.responseData.results, function(key, node) {
						count++;
						jQuery('<a></a>').attr('href', node.url).html(node.title).appendTo($resultcontainer);
						$resultcontainer.append('<br />');
						jQuery('<p></p>').html(node.content).appendTo($resultcontainer);
						$resultcontainer.append('<br />');
					});
					if (count == 0) {
						$resultcontainer.append('Ingen Google info hittades');
					}
				},
				error: function(XMLHttpRequest, textstatus) {
					var $resultcontainer = jQuery('#resultweb .mse-boxbody');
					$resultcontainer.html('');
					if (textstatus == 'timeout') {
						$resultcontainer.append('Ingen Google info hittades');
					} else {
						$resultcontainer.append('Ingen Google info hittades');
					}
				},
				complete: function() {
				}
			});
		}
		
		if (jQuery('#checkbox-blog').is(':checked')) {
			
			var $resultcontainer = jQuery('#resultblog');
			$resultcontainer.show();
			//Blog
			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=googleblog&v=1.0&h1=sv&rsz=8&q="'+value+'"', 
				type: 'GET',
				dataType: 'json',
				timeout: 10000,
				success: function(rsp) {
					var $resultcontainer = jQuery('#resultblog .mse-boxbody');
					var count = 0;
					$resultcontainer.html('');
					jQuery.each(rsp.responseData.results, function(key, node) {
						count++;
						jQuery('<a></a>').attr('href', node.postUrl).html(node.title).appendTo($resultcontainer);
						$resultcontainer.append('<br />');
						jQuery('<p></p>').html(node.content).appendTo($resultcontainer);
						$resultcontainer.append('<br />');
					});
					if (count == 0) {
						$resultcontainer.append('Inga blogginlägg hittades');
					}
				},
				error: function(XMLHttpRequest, textstatus) {
					var $resultcontainer = jQuery('#resultblog .mse-boxbody');
					$resultcontainer.html('');
					if (textstatus == 'timeout') {
						$resultcontainer.append('Inga blogginlägg hittades');
					} else {
						$resultcontainer.append('Inga blogginlägg hittades');
					}
				},
				complete: function() {
				}
			});
		}		
		
		if (jQuery('#checkbox-image').is(':checked')) {
			
			var $resultcontainer = jQuery('#resultimage');
			$resultcontainer.show();
			//Image
			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=googleimage&v=1.0&h1=sv&rsz=8&q="'+value+'"', 
				type: 'GET',
				dataType: 'json',
				timeout: 10000,
				success: function(rsp) {
					var $resultcontainer = jQuery('#resultimage .mse-boxbody');
					var count = 0;
					$resultcontainer.html('');
					jQuery.each(rsp.responseData.results, function(key, node) {
						count++;
						$resultcontainer.append('<img height="100px" src="'+node.url+'" /> ');
					});
					if (count == 0) {
						$resultcontainer.append('Inga bilder hittades');
					}
				},
				error: function(XMLHttpRequest, textstatus) {
					var $resultcontainer = jQuery('#resultimage .mse-boxbody');
					$resultcontainer.html('');
					if (textstatus == 'timeout') {
						$resultcontainer.append('Inga bilder hittades');
					} else {
						$resultcontainer.append('Inga bilder hittades');
					}
				},
				complete: function() {
				}
			});
		}	

		
		if (jQuery('#checkbox-video').is(':checked')) {
			
			var $resultcontainer = jQuery('#resultvideo');
			$resultcontainer.show();
			//Video
			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=googlevideo&v=1.0&h1=sv&rsz=8&q="'+value+'"', 
				type: 'GET',
				dataType: 'json',
				timeout: 10000,
				success: function(rsp) {
					var $resultcontainer = jQuery('#resultvideo .mse-boxbody');
					var count = 0;
					$resultcontainer.html('');
					jQuery.each(rsp.responseData.results, function(key, node) {
						count++;
						if (key<3){
							$resultcontainer.append('<iframe width="250" height="200" src="'+node.playUrl.replace('autoplay=1', 'autoplay=0')+'" frameborder="0" allowfullscreen></iframe>');
							$resultcontainer.append('<br /> <br />');
						}
					});
					if (count == 0) {
						$resultcontainer.append('Inga videor hittades');
					}
				},
				error: function(XMLHttpRequest, textstatus) {
					var $resultcontainer = jQuery('#resultvideo .mse-boxbody');
					$resultcontainer.html('');
					if (textstatus == 'timeout') {
						$resultcontainer.append('Inga videor hittades');
					} else {
						$resultcontainer.append('Inga videor hittades');
					}
				},
				complete: function() {

				}
			});
		}	
		
		if (jQuery('#checkbox-maps').is(':checked')) {
			
			var $resultcontainer = jQuery('#resultmaps');
			$resultcontainer.show();
			//Maps
			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=googlemaps&sensor=false&address='+value, 
				type: 'GET',
				dataType: 'json',
				timeout: 10000,
				success: function(rsp) {

					var string = 'http://maps.google.com/maps/api/staticmap?center=gdansk&zoom=3&size=250x200&maptype=roadmap&sensor=false';
					
					var count = 0;
					jQuery.each(rsp.results, function(key, node) {
						count++;
						string += '&markers=color:blue%7Clabel:S%7C'+node.geometry.location.lat+','+node.geometry.location.lng;
					});
					
					if (count == 0) {
						jQuery('#resultmaps .mse-boxbody').html('Ingen kartträff funnen.');
					} else {
						jQuery('#resultmaps .mse-boxbody').html('<img src="'+string+'" />');
					}
			
				},
				error: function(XMLHttpRequest, textstatus) {

					if (textstatus == 'timeout') {

					} else {

					}
				},
				complete: function() {

				}
			});
		}	
		
		if (jQuery('#checkbox-wiki').is(':checked')) {
			
			
			var $resultcontainer = jQuery('#resultwiki');
			$resultcontainer.show();	
			
			//Wikipedia
			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=wikipedia&action=opensearch&format=xml&search='+value, 
				type: 'GET',
				dataType: 'xml',
				timeout: 10000,
				success: function(rsp) {
					var $rsp = jQuery(rsp);
					var $resultcontainer = jQuery('#resultwiki .mse-boxbody');
					var count = 0;
					
					$resultcontainer.html('');
					$rsp.find('Item').each(function() {
						count++;
						if (count<8) {
						$resultcontainer.append('<a href="'+jQuery(this).find('Url').text()+'">'+jQuery(this).find('Text').text()+'</a>');
						$resultcontainer.append('<br />');
						
						$resultcontainer.append('<p>'+jQuery(this).find('Description').text()+'</p>');
						$resultcontainer.append('<br />');
						}

					});
					if (count == 0) {
						$resultcontainer.append('Ingen info på Wikipedia hittades');
					}
				},
				error: function(XMLHttpRequest, textstatus) {
					var $resultcontainer = jQuery('#resultwiki .mse-boxbody');
					$resultcontainer.html('');
					if (textstatus == 'timeout') {
						$resultcontainer.append('Ingen info på Wikipedia hittades');
					} else {
						$resultcontainer.append('Ingen info på Wikipedia hittades');
					}
				},
				complete: function() {

				}
			});
		}	
		
		if (jQuery('#checkbox-facebook').is(':checked')) {
			
			jQuery('#resultfb_post').show();
			jQuery('#resultfb_page').show();
			jQuery('#resultfb_group').show();
			
			//Facebook Post
			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=facebook&type=post&q='+value, 
				type: 'GET',
				dataType: 'json',
				timeout: 10000,
				success: function(rsp) {
					var $resultcontainer = jQuery('#resultfb_post .mse-boxbody');
					var count = 0;
					$resultcontainer.html('');
					jQuery.each(rsp.data, function(key, node) {
						count++;
						if (node.from.id != '' && node.from.name != '' && node.message != 'undefined') {
							if (key<10){
							$resultcontainer.append('<p><a href="http://www.facebook.com/profile.php?id='+node.from.id+'">'+node.from.name+'</a><br />'+node.message+'</p>');
							}
						}
						
					});
					if (count == 0) {
						$resultcontainer.append('Inga öppna Facebookposter hittades');
					}
					
				},
				error: function(XMLHttpRequest, textstatus) {
					var $resultcontainer = jQuery('#resultfb_post .mse-boxbody');
					$resultcontainer.html('');
					if (textstatus == 'timeout') {
						$resultcontainer.append('Inga öppna Facebookposter hittades');
					} else {
						$resultcontainer.append('Inga öppna Facebookposter hittades');
					}
				},
				complete: function() {

				}
			});
			
			//Facebook Page
			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=facebook&type=page&q='+value, 
				type: 'GET',
				dataType: 'json',
				timeout: 10000,
				success: function(rsp) {

					var $resultcontainer = jQuery('#resultfb_page .mse-boxbody');
					$resultcontainer.html('');
					jQuery.each(rsp.data, function(key, node) {
						if (key<10){
						$resultcontainer.append('<a href="http://www.facebook.com/pages/'+node.name+'/'+node.id+'">'+node.name+'</a>');
						$resultcontainer.append('<br />');
						}
					});
					
				},
				error: function(XMLHttpRequest, textstatus) {
					var $resultcontainer = jQuery('#resultfb_page .mse-boxbody');
					$resultcontainer.html('');
					if (textstatus == 'timeout') {

					} else {

					}
				},
				complete: function() {

				}
			});
			
			//Facebook Group
			jQuery.ajax({ 
				url: '/wp-content/themes/skill/mse-search.php?searchtype=facebook&type=group&q='+value, 
				type: 'GET',
				dataType: 'json',
				timeout: 10000,
				success: function(rsp) {

					var $resultcontainer = jQuery('#resultfb_group .mse-boxbody');
					var count = 0;
					$resultcontainer.html('');
					jQuery.each(rsp.data, function(key, node) {
						count++;
						if (key<20){
						$resultcontainer.append('<a href="http://www.facebook.com/group.php?gid='+node.id+'">'+node.name+'</a>');
						$resultcontainer.append('<br /><br />');
						}
					});
					if (count == 0) {
						$resultcontainer.append('Inga Facebookgrupper hittades');
					}
					
				},
				error: function(XMLHttpRequest, textstatus) {
					var $resultcontainer = jQuery('#resultfb_group .mse-boxbody');
					$resultcontainer.html('');
					if (textstatus == 'timeout') {
						$resultcontainer.append('Inga Facebookgrupper hittades');
					} else {
						$resultcontainer.append('Inga Facebookgrupper hittades');
					}
				},
				complete: function() {

				}
			});
			
		}
		}
		
	}); //

});
