/* jQuery CooQuery Plugin v2 (minified) - http://cooquery.lenonmarcel.com.br/
Copyright 2009, 2010 Lenon Marcel
Dual licensed under the MIT and GPL licenses.
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html */
(function($){$.setCookie=function(name,value,options){if(typeof name==='undefined'||typeof value==='undefined')
return false;var str=name+'='+encodeURIComponent(value);if(options.domain)str+='; domain='+options.domain;if(options.path)str+='; path='+options.path;if(options.duration){var date=new Date();date.setTime(date.getTime()+options.duration*24*60*60*1000);str+='; expires='+date.toGMTString();}
if(options.secure)str+='; secure';return document.cookie=str;};$.delCookie=function(name){return $.setCookie(name,'',{duration:-1});};$.readCookie=function(name){var value=document.cookie.match('(?:^|;)\\s*'+name.replace(/([-.*+?^${}()|[\]\/\\])/g,'\\$1')+'=([^;]*)');return(value)?decodeURIComponent(value[1]):null;};$.CooQueryVersion='v 2.0';})(jQuery);

function success(id, responseText, statusText) {
	var msg = "";
    if (statusText == "success") {
    	if (responseText.err) {
    		$("#" + id).html('<div class="error">' + responseText.err + '</div>');
    	} else {
    		$("#" + id).html('<div class="message">' + responseText.res + '</div>');
    	}
    } else {
    	$("#" + id).html('<div class="error">' + responseText.err + '</div>');
    }
}
function no() {
	alert("We are not support your browser yet.");
}

function getWeather() {
	$.ajax({
	  type: "GET",
	  url: "weather",
	  //dataType: "json",
	  beforeSend: function () {
			//$('.move p').html("Loading...");
	  },
	  success: function (responseText) {
		  $('#climate').html(responseText);
		  setTimeout(getWeather, 7000);
		}
	});
}

function disableSelection(target) {
	if (typeof target.onselectstart != "undefined") //IE route
		target.onselectstart = function(){return false}
	else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
		target.style.MozUserSelect = "none"
	else //All other route (ie: Opera)
		target.onmousedown = function(){return false}
	target.style.cursor = "default"
}


$(document).ready(function() {
	$("a").click(function(){
		var id = $(this)[0].id;
		if (id.substr(0, 3) == 'ba_') {
			$.ajax({
			  type: "POST",
			  url: "ajax_click",
			  dataType: "json",
			  data: ({id : id}),
			  success: function (responseText) {
					if (responseText) {
						alert(responseText);
					}
				}
			});
		}
	});	
	disableSelection(document.body);
	$.ajax({
	  type: "GET",
	  url: "get.rates",
	  //dataType: "script",
	  success: function (responseText) {
			if (responseText) {
				//alert("Can't update rates");
			}
		}
	});
	
	$.ajax({
	  type: "GET",
	  url: "get.stock",
	  dataType: "json",
	  beforeSend: function () {
			$('.move p').html("Loading...");
	  },
	  success: function (responseText) {
		  $('.move p').html();
			if (responseText) {
				$('.move p').html(responseText.data);
				$('#info').crawlLine({crawElement:'.move'});
			}
		}
	});
	
	getWeather()
	
	if (typeof(inputPlaceholder) != 'undefined') {
		inputPlaceholder('contact_name');
		inputPlaceholder('contact_email');
		inputPlaceholder('contact_subj');
		inputPlaceholder('contact_comments');
	}
	
	$("#btn1").click(function(){
		$('#currencies-table-block').show();
		$('#layout').show();
		$('#thecode').hide();
		$('#currencies-converter-block').hide();
		$('#btn1-container').removeClass('tab-off').addClass('tab-on').next().removeClass('tab-on').addClass('tab-off');
	});
	$("#btn2").click(function(){
		$('#currencies-converter-block').show();
		$('#layout').hide();
		$('#thecode').show();
		$('#currencies-table-block').hide();
		$('#btn2-container').removeClass('tab-off').addClass('tab-on').prev().removeClass('tab-on').addClass('tab-off');
	});
	
	var options = { 
        target:        "#qcc_convert_result",   // target element(s) to be updated with server response 
        beforeSubmit:  function (formData, jqForm, options) { 
			$("#qcc_convert_result").html('');
    	},
        success:       function (responseText, statusText) {
    		var msg = "";
    	    if (statusText == "success") {
    	    	if (responseText.err) {
    	    		alert(responseText.err);
    	    	} else {
    	    		$("#qcc_convert_result").html(responseText.res);
    	    	}
    	    } else {
    	    	alert('Some error happens. We know.');
    	    }
    	},
 
        // other available options: 
        url:       "ajax_quickconvert.html",
        type:      "post",
        dataType:  "json"
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 

    $("#qcc_form").submit(function() { 
        $(this).ajaxSubmit(options); 
 		return false; 
    }); 
    


    
    
    $("#form_contact").submit(function() { 
        $(this).ajaxSubmit({ 
            target:        "#contact_result",   // target element(s) to be updated with server response 
            beforeSubmit:  function (formData, jqForm, options) { 
    			$("#contact_result").html('');
        	},
            success:       function (responseText, statusText) {
        		success('contact_result', responseText, statusText);
        		// -- clear form is success
    			if (!responseText.err) {
        			$( "#form_contact" )[ 0 ].reset();
        		}
        	},
     
            url:       "ajax_contactus.html",
            type:      "post",
            dataType:  "json"
            
        }); 
 		return false; 
    });
    
    $("#qcc_convert").click(function(){
		$("#qcc_form").submit();
	});	
    $("#qcc_convert_inverse").click(function(){
    	var buf_from = $('select[name=convert_from]').val();
        var buf_to = $('select[name=convert_to]').val();
        $('select[name=convert_from] > option[value='+buf_to+']').attr('selected',true);
        $('select[name=convert_to] > option[value='+buf_from+']').attr('selected',true);
        $("#qcc_form").submit();
	});	
    
    $("#submit_subscriber").click(function(){
    	$.ajax({
		  type: "POST",
		  url: "ajax_subscribe",
		  dataType: "json",
		  data: ({imasubscriber : $("#imasubscriber").val()}),
		  success: function (responseText, textStatus) {
    			success('subscribe_result', responseText, textStatus);
			}
		});
	});	
    
    $("#submit_contact").click(function(){
    	$("#form_contact").submit();
	});
});