// Internet Explorer 6
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

// exactly Internet Explorer 7
var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/;

// at least Internet Explorer 7
var gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/;

// any Internet Explorer (thanks to Dean)
var isMSIE = /*@cc_on!@*/false;

$(document).ready(function() {
		onloadfix(); 
		
		Shadowbox.init();
	} 
);


function fixfonts() {
	if (!IE6)
	{
	Cufon.replace('#tools',{hover: true});
	Cufon.replace('.toptit');
	//Cufon.replace('.outnow');
	Cufon.replace('.biglist');
	Cufon.replace('.rightblock h2');
	Cufon.replace('a.gobutt');
	Cufon.replace('a.back');
	Cufon.replace('a.gobutt2');
	Cufon.replace('div.item h1');
	Cufon.replace('div.item h2');
	Cufon.replace('h1.mainlib');
	Cufon.replace('h2.red');

	}
}

function checkbuynowcartbutton () {
	if ($('#cart_container ul').children().size() == 0) $('#buynowcart').hide(); else $('#buynowcart').show();
}

function onloadfix() {

	$('.lib2lev').hover( function () {
			$(this).children('ul').show();
		},function () {
			$(this).children('ul').hide();
		}
	);

	var ie6 = document.getElementById && document.all&&(navigator.appVersion.indexOf("MSIE 6.")>=0);
	if (ie6)
	{
		setTimeout('fixfonts();',1000)
	} else {
		fixfonts();
	}

	checkbuynowcartbutton ();
	setWriteHere("writehere");


}

function setWriteHere(search) {

	$("input[rel^="+search+"]").click( function () {
		var ar = $(this).attr('rel').split('|');
		if($(this).val()==ar[1]) { $(this).val(""); $(this).attr('class',ar[3]); }
	} );
	$("input[rel^="+search+"]").blur( function () {
		var ar = $(this).attr('rel').split('|');
		if($(this).val()=="") { $(this).val(ar[1]); $(this).attr('class',ar[2]); }
	} );
	$("input[rel^="+search+"]").each( function () { $(this).blur(); } );

}


function additem(id,tipo) {
	
	var dataString = $.toJSON( { 'id': id, 'tipo': tipo } ); 
	  $.post('res/inc/ajax.additem.php', {data: dataString}, function(res){ 
		if (res!="OK") { alert(dump(res)); }
		showcart();
	  }); 

}

function removeitem(id,tipo) {
	
	var dataString = $.toJSON( { 'id': id, 'tipo': tipo } ); 
	  $.post('res/inc/ajax.removeitem.php', {data: dataString}, function(res){ 
		if (res!="OK") { alert(dump(res)); }
		showcart();
	  }); 

}


function showcart() {
	
	$.get("res/inc/ajax.showcart.php", function(data) {
			$('#cart_container').html(data);
			checkbuynowcartbutton ();
		}
	);
	
}

function showcart_riepilogo() {

	shipping_country = $('#scountry :selected').val() ? $('#scountry :selected').val() : $('#pcountry :selected').val();

	$.get("res/inc/ajax.showcart-riepilogo.php?shipping_country="+escape(shipping_country), function(data) {
			$('#cart_container').html(data);
			if ($('#cart_container table').children().size() == 0) $('#buynowcart_riepilogo').hide(); else $('#buynowcart_riepilogo').show();
			if (data.indexOf('>BOOK<') == -1) $('#shipping_block').hide()
		}
	);
	
}
function removeitem_riepilogo(id,tipo) {
	
	var dataString = $.toJSON( { 'id': id, 'tipo': tipo } ); 
	  $.post('res/inc/ajax.removeitem.php', {data: dataString}, function(res){ 
		if (res!="OK") { alert(dump(res)); }
		showcart_riepilogo();
	  }); 

}

/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function checkradio() {
	if ($('#btype1').attr("checked"))
	{
		$('#labelsurname').hide();
		$('#bsurname').hide();
		$('#bsurname').val("");
	} else {
		$('#labelsurname').show();
		$('#bsurname').show();
	}

}

function newsletter_send(op) {
	var msg = "";
	var email = $('#hjitly-hjitly').val();
	if (email!="") {
		var email_true = is_email(email);
		if (email_true) {
			if (op=="subscribe") {
				$('#subForm').attr('action','http://editorialelotus.createsend.com/t/r/s/hjitly/');
			} else if (op=="unsubscribe") {
				$('#subForm').attr('action','http://editorialelotus.createsend.com/t/r/u/hjitly/');
			}
			$('#subForm').submit();
		} else {
			msg = "your email is not valid."
		}
	} else {
		msg = "insert your email";
	}
	
	if (msg!="") {
		$('#error').html(msg).fadeIn('fast').fadeOut('fast').fadeIn('fast').fadeOut('fast').fadeIn('fast');
	}
}

function is_email($e) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   	if(reg.test($e) == false) {
      return false;
   	}
	return true;
}