/*
	omedia.ge website javascript stuff
			    © omedia, 2008
*/



//
// open links with rel="external" in new window
// (tweak to preserve valid *strict* markup)
//

  $(document).ready(function(){
    $("a[@rel^='external']").attr('target','_blank');
    
    $(".codeblock a").attr('target','_blank');
  });


//
// get companies list (again and again and again...)
//

	$(document).ready(function(){
		$("#reloadcompanies").click(function(){
			$.post(site_url+"ajax.php", {mode:"companies"}, function(data){
				$("#companieslist").html(data);
			});
			return false;
		});
	});
	


//
// image preloading function. short and sweet :) © wordpress ;)
//
	
	$.preloadImages = function(){
		for(var i = 0; i<arguments.length; i++){
			jQuery("<img>").attr("src", arguments[i]);
		}
	}
	

//
// konami andadasi :D
//

	$(document).ready(function(){

        var pressedKeys = [];
        var konami = "38,38,40,40,37,39,37,39,66,65,13";
        
        $(document).keydown(function(e) {
          pressedKeys.push(e.keyCode);
          if(pressedKeys.toString().indexOf( konami ) >= 0) {
            $(document).unbind('keydown', arguments.callee);
            $("#andadasi").click();
          }
        });

	});


//
// initializing georgian keyboards by code.ge
//


	$(document).ready(function() {
	
		if(typeof englishPage == 'undefined') {
	
			// only if this isn't an english page
			GeoKBD.map( 'tellmeform', ['whoareyou', 'whattosay'] );
			GeoKBD.map( 'addcommentform', ['author', 'comment'] );
			GeoKBD.setGlobalHandler(true);
		
		}
		
	});
	


//
// making false links to do nothing on click
//


	$(document).ready(function() {
	
		$(".fakelink").click(function() {
			$(this).css("outline", "0");
			return false;
		});
		
	});


	
	
//
// open under the sea (the secret) area! shhhh!...
//

	$(document).ready(function(){
		
		// first of all, preload ocean, fish and omedians
		if(typeof englishPage == 'undefined') {
			// preload georgian ocean
			$.preloadImages ( site_url + "images/quiet_water.png", site_url + "images/secret_ocean.png" );
		} else {
			// preload english ocean
			$.preloadImages ( site_url + "images/quiet_water.png", site_url + "images/secret_ocean_eng.png" );
		}
		
		// if JS is enabled, show the link paragraph
		$("#footer p.andadasi_container").show();
		
		// scroll, when click is simulated
		$(".andadasi_link").localScroll();
		
		// show, when clicked
		$("#andadasi").click(function(){
			$("#secret_wrapper").show(1, function(){
				// simulating... ;;)
				$(".andadasi_link a").click();
				
				// inserting flash  :)
				$('#music').flash(
					{
						params:
						{
							wmode: "transparent"
						},
						src: site_url + 'images/andadasi.swf',
						width: 18,
						height: 18
					},
					{
						version: '7.0.0.0',
						update: false
					}
				);
			});
			// get rid of the link
			$(this).remove();
			// track the click
			pageTracker._trackPageview("/andadasi" );
			return false;
		});
		
	});
	
	
	
	
//
// contact page area
//

if (typeof contactPage != 'undefined') {

	$(document).ready(function(){

		// preload beautiful loading.gif
		$.preloadImages ( site_url + "images/loading.gif" );
			
		// map localscroll
		$("li.adr").localScroll();
		
		// swap map
		var displaingAero = false;
		$("#map input").click(function(){
			if(displaingAero) {
				$("#map img").removeClass("photo");
				displaingAero = false;
			} else {
				$("#map img").addClass("photo");
				displaingAero = true
			}
		});
		
		// simulate label for map
		$("#map span").click(function(){
			$("#map input").click();
			return false;
		});
		
		// swap map on map-click
		$("#map img").css("cursor", "pointer").click(function(){
            $("#map input").click();
        });
		
	
		// show the form (if we have js)
		$(".contact .contactform").show();

	
		// if form is clicked
		$("#tellmeform .wannabesubmit").click(function() {
		
			var allOK = true;
		
			// validate form fields
			
			if($.trim($("#tellmeform .whattosay").val()).length <= 0) {
			
				$("#tellmeform .whattosay").addClass("highlight");
				allOK = false;
			
			}
			if($.trim($("#tellmeform .whoareyoutext").val()).length <= 0) {
			
				$("#tellmeform .whoareyoutext").addClass("highlight");
				allOK = false;
			
			}
			
			// if all is ok, do ajax
			if(allOK) {
				
				// get values
				var name = $("#tellmeform .whoareyoutext").val();
				var email = $("#tellmeform .yomail").val();
				var text = $("#tellmeform .whattosay").val();
			
				// hide form and show loading
				$("#tellmeform").css("display", "none");
				$(".envelope .loading").css("display", "block");
				
				// send letter after 2 secs
				setTimeout(function(){
					$.post(site_url+"ajax.php", {mode:"contact", whoareyou:name, yomail:email, whattosay:text}, function(data){
						
						// on complete, hide loader and show thankyou
						$(".envelope .loading").css("display", "none");
						$(".envelope .thankyou").css("display", "block");
						
					});
				}, 2000);
				
			} //
			
			return false;
			
		});
		
		
		// remove validation errors on click
		$("#tellmeform .text").focus(function() {
			$(this).removeClass("highlight");
		});
	
	
	});

}



	
//
// comments page area
//

if (typeof commentPage != 'undefined') {

	$(document).ready(function(){

		// init
		
		// show form
		$("#addcommentform").show();
	
		// preload beautiful loading.gif
		$.preloadImages ( site_url + "images/loading.gif" );
		
		// stuff
		
		// get selected text
		$("#quoteselection").click(function(){
	
			var selTxt = '';
			
			if (window.getSelection) {
				selTxt = window.getSelection();
			} else if (document.getSelection) {
				selTxt = document.getSelection();
			} else if (document.selection) {
				selTxt = document.selection.createRange().text;
			}
			
			// add html
			selTxt = "\n<blockquote>" + selTxt + "</blockquote>\n";
			
			$("#textfield").val( $("#textfield").val() + selTxt );
			$("#textfield").focus();
			
			return false;
			
		});
		
		
		// try to do comment posting
		$("#addcommentform .submit").click(function() {
		
			var allOK = true;
							
			// validate form fields
			
			if($.trim($("#namefield").val()).length <= 0) {
			
				$("#namefield").addClass("highlight");
				allOK = false;
			
			}
			if($.trim($("#emailfield").val()).length <= 0) {
			
				$("#emailfield").addClass("highlight");
				allOK = false;
			
			}
			if($.trim($("#textfield").val()).length <= 0) {
			
				$("#textfield").addClass("highlight");
				allOK = false;
			
			}
			
			// if all is ok, do ajax
			if(allOK) {
				
				// get values
				var name = $("#namefield").val();
				var email = $("#emailfield").val();
				var www = $("#wwwfield").val();
				var text = $("#textfield").val();
				var postid = $("#postidfield").val();
				var postslug = $("#postslugfield").val();
			
				// hide form and show loading
				$("#addcommentform").css("display", "none");
				$(".addcomment .loading").css("display", "block");
				
				// send letter after 2 secs
				setTimeout(function(){
					$.post(site_url+"ajax.php", {mode:"comment", author:name, mail:email, www:www, comment:text, postid:postid, postslug:postslug}, function(data){
						
						// on complete, hide loader and show thankyou
						$(".addcomment .loading").css("display", "none");
						$(".addcomment .thankyou").css("display", "block");
						
						// add to .comments ul
						if( $(".comments").length > 0 ) {
						
							// if exists, add this
							$(".comments").append( data );
							
							// and increase comment count in title
							var totalComments = $(".commentstitle span").html();
							totalComments = parseInt(totalComments) + 1;
							$(".commentstitle span").html(totalComments);
						
						} else {
						
							//if not, insert comment wrapped in ul
							$(".addcomment").before( '<h2 class="commentstitle">1 კომენტარი:</h2> <ul class="comments"> ' + data + ' </ul>' );
						
						}
						
					});
				}, 1300);
				
			} //
			
			return false;
			
		});


		// remove validation errors on click
		$("#addcommentform .text").focus(function() {
			$(this).removeClass("highlight");
		});
	






	});
	
}






	
//
// bonus page area
//

if (typeof bonusPage != 'undefined') {

	$(document).ready(function(){
		
		// define current page resolution
		var userRes = screen.width + ' × ' + screen.height + ' px';
		
		// walk through wallpapers
		$(".downloadlist").each(function() {
			
			// walk through wallpaper resolutions
			$("a", this).each(function(){
				
				if($(this).html() == userRes) {
					// mark as recommended
					$(this).addClass("recomended");
					if(site_lang == 'geo') {
						$(this).attr("title", "რეკომენდირებულია თქვენი მონიტორისთვის");
					} else {
						$(this).attr("title", "Recommended for your screen");
					}
					return false;
				}
				
			});
			
		});


	});
}






	
//
// bonus page area
//

if (typeof homePage != 'undefined') {

	$(document).ready(function(){
		/*
		// un-hide splashscreen div
		$("#splashscreen").show();

		// inserting flash  :)
		$('#splashscreen').flash(
			{
				src: site_url + 'images/splashes/iphone.swf',
				width: 702,
				height: 300,
				bgcolor: "#fbf15d"
			},
			{
				version: '8.0.0.0',
				update: false
			}
		);
		*/
		
		
	});
}






	
	
	
	
	
