/* Select an item and submit the form to add the selected item to cart */
function checkAndSubmit(checkbox, form) {
	document.getElementById(checkbox).checked=true;
	document.getElementById(form).submit();
}

/* Show/hide login form */
function showHideLogin() {
	if (typeof(jQuery) == 'undefined')
		return false;
	
	jQuery("#loginform").toggle();
}

/* Check and Uncheck all checkboxes */
function divCheckUncheckBoxes(div, check) {
	div = document.getElementById(div);
	var chks = div.getElementsByTagName("*");
	for (var i=0, l=chks.length; i<l; i++)
		if (chks[i].type == "checkbox") 
			chks[i].checked = check; 
}

function translate(lang, originaldiv, translationdiv) {
	if (typeof(google.language) == 'undefined')
		return false;
	
   	var source = document.getElementById(originaldiv).innerHTML;
   	var len = source.length;

   	// Google Language API accepts 500 characters per request 
   	var words = 500;
   	var sourcelang = "en";
   	document.getElementById(translationdiv).innerHTML = "";

   	for(i=0; i<=(len/words); i++) {
   		google.language.translate (source.substr(i*words, words), "en", lang, function (result) {
     		if (!result.error) {
     			document.getElementById(translationdiv).innerHTML
		           = document.getElementById(translationdiv).innerHTML
    		       + result.translation;
    		}
    	});
    }  

  	document.getElementById(originaldiv).style.display = 'none';
  	return false;
}

jQuery(document).ready(function() {
	// Frame containers expand/colapse
	if (typeof(jQuery(this).fcToggle) != 'undefined')
		jQuery(this).find(".fc-title").fcToggle();

	// Mail me emails
	if (typeof(jQuery(this).mailme) != 'undefined')
		jQuery(this).find('.mailme').mailme();
			
	// Confirm a link submittion
	jQuery(this).find('.confirm-link').click(function() {
		return confirm('Please confirm your action: '+jQuery(this).attr('title')+'?!');
	});
			
	// Calendar Inputs
	if (typeof(jQuery(this).datepicker) != 'undefined') {
		jQuery(this).find(".calendar-input").each(function(){
			if (jQuery(this).is('.timestamp')) {
				d = new Date();
				dateformat = 'yy-mm-dd '+d.getHours()+':'+d.getMinutes();
			} else {
				dateformat = 'yy-mm-dd';
			}
			
			jQuery(this).datepicker({dateFormat: dateformat});
		});
	
		jQuery(this).find(".show-calendar-input").click(function(){
			jQuery(this).parent().find('input').focus();
			return false;
		});
		
		jQuery(this).find(".clear-calendar-input").click(function(){
			jQuery(this).parent().find('input').val('');
			return false;
		});
	}
			
	// LightBox
	if (typeof(jQuery(this).lightBox) != 'undefined') {
		jQuery(function() {
			var boxen = [];
			jQuery('a[rel^=lightbox]').each(function() {
				if (jQuery.inArray(jQuery(this).attr('rel'),boxen)) 
					boxen.push(jQuery(this).attr('rel'));
			});
			jQuery(boxen).each(function(i,val) {
				jQuery('a[rel='+val+']').lightBox({maximumImageWidth: 0});
			});
		});
	}
	
	jQuery('.update-select-checkbox').change(function() {
		updateid = this.name.replace(/.*\[(.*?)\]/, "$1");
		obj = jQuery('#update-options-'+updateid+' a.fc-title');
		
		if ((this.checked && !obj.is('.expanded')) || (!this.checked && obj.is('.expanded')))
			obj.click(); 
	});
	
	jQuery('.import-update-pictures').click(function() {
		if (!jQuery(this).is('.expanded')) {
			jQuery(this).parent().find('img').each(function() {
				jQuery(this).parent().lightBox({maximumImageWidth: 0});
				this.src = jQuery(this).attr('rel');
			});
		}
	});
});