var myvar;
var APSOLUTE_URL = 'http://888.hostxtremdns.com/zidoo/msb2catalog/';
function menuinit(i) 
{
	//alert('x');
	if (i)
	{
		myvar = i;
		document.getElementById('scat-'+i).style.display = 'block';
		document.getElementById('plus-'+i).src = APSOLUTE_URL+"images/zidoo/minus.jpg";
	}
}


function menuexpand (i) 
{
        menuinit();
    if (myvar == i) 
    {
		document.getElementById('scat-'+i).style.display = 'none';
		document.getElementById('plus-'+i).src = APSOLUTE_URL+"images/zidoo/plus.jpg";
		myvar = 0;
	}
        else  
    {
    	if (myvar)
	    { 
	    	document.getElementById('scat-'+myvar).style.display = 'none';
			document.getElementById('plus-'+myvar).src = APSOLUTE_URL+"images/zidoo/plus.jpg";
	    }
		document.getElementById('scat-'+i).style.display = 'block';
		document.getElementById('plus-'+i).src = APSOLUTE_URL+"images/zidoo/minus.jpg";
		myvar = i;
	}
}








function function_exists( function_name ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Steve Clay
	// +   improved by: Legaev Andrey
	// *     example 1: function_exists('isFinite');
	// *     returns 1: true


	if (typeof function_name == 'string'){
		return (typeof window[function_name] == 'function');
	} else{
		return (function_name instanceof Function);
	}
}

if (!function_exists('btoa')) {
	function btoa( data ) {
		var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

		do { // pack three octets into four hexets
			o1 = data.charCodeAt(i++);
			o2 = data.charCodeAt(i++);
			o3 = data.charCodeAt(i++);

			bits = o1<<16 | o2<<8 | o3;

			h1 = bits>>18 & 0x3f;
			h2 = bits>>12 & 0x3f;
			h3 = bits>>6 & 0x3f;
			h4 = bits & 0x3f;

			// use hexets to index into b64, and append result to encoded string
			enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
		} while (i < data.length);

		switch( data.length % 3 ){
			case 1:
			enc = enc.slice(0, -2) + '==';
			break;
			case 2:
			enc = enc.slice(0, -1) + '=';
			break;
		}

		return enc;
	}
}

if (!function_exists('atob')) {
	function atob( data ) {
		var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

		do {  // unpack four hexets into three octets using index points in b64
			h1 = b64.indexOf(data.charAt(i++));
			h2 = b64.indexOf(data.charAt(i++));
			h3 = b64.indexOf(data.charAt(i++));
			h4 = b64.indexOf(data.charAt(i++));

			bits = h1<<18 | h2<<12 | h3<<6 | h4;

			o1 = bits>>16 & 0xff;
			o2 = bits>>8 & 0xff;
			o3 = bits & 0xff;

			if (h3 == 64)      enc += String.fromCharCode(o1);
			else if (h4 == 64) enc += String.fromCharCode(o1, o2);
			else               enc += String.fromCharCode(o1, o2, o3);
		} while (i < data.length);

		return enc;
	}
}
/*
$(document).ready(function() { 
	$('#addEmail').ajaxSubmit(
	{
	    success:  function(t) 
	    {
		    $('#emailRet').text(t);
  		}
	}
	);
});
*/
function submitEmail()
{
	if (emailOK(document.email_form.email))
	//if(document.email_form.email.value=='')
	{
		$('#addEmail').ajaxSubmit(
		{
		    success:  function(t) 
		    {
			    $('#emailRet').text(t);
	  		}
		}
		);
	}
	else
	{
		$('#emailRet').text('Neispravan email');
	}
}

function emailOK(e)
{
    var at = e.value.indexOf('@');
    var dot = e.value.lastIndexOf('.');
    var space = e.value.indexOf(' ');
    var len = e.value.length - 1;

    return ((at > 1) && (dot >= at + 1) && (dot < len) && (space  == -1)) ? true : false;
}

function expand(id, path)
{
	$('#premium-'+id).load(path + 'deamon.php?show=CONTACT&id='+id);
}


function submitContact(id, path)
{
	var formaid = 'cf-'+id;
	var forma = document.getElementById(formaid);
	if(forma.from.value=='')
		{
			$('#notice-'+id).text("Niste popunili polje \"Od\"");
			forma.from.focus();
			return (false);
		} 
	if(forma.subject.value=='')
		{
			$('#notice-'+id).text("Niste popunili polje \"Naslov\"");
			forma.subject.focus();
			return (false);
		} 
	if(forma.body.value=='')
		{
			$('#notice-'+id).text("Niste popunili polje \"Poruka\"");
			forma.body.focus();
			return (false);
		} 
	if(forma.captcha.value=='')
		{
			$('#notice-'+id).text("Upisite kod sa slike");
			forma.captcha.focus();
			return (false);
		} 
	$('#cf-'+id).ajaxSubmit(
	{
	    success:  function(t) 
	    {
		    $('#notice-'+id).text(t);
		    if (t == 'Pogresan kod')
				forma.captcha.focus();
			else
			{
				$('#send-'+id).attr('disabled', 'disabled');
				//closeContact(id, path);
			}
  		}
	}
	);
	
	
	/*(function() 
	{ 
		//closeContact(id, path);
	});*/
}


function submitOglasavanje(id,path)
{
	var forma = document.getElementById(id);
	if(forma.email.value!='' && !emailOK(forma.email))
		{
			$('#notice-'+id).text('Upišite ispravnu e-mail adresu');
			forma.email.focus();
			return (false);
		}
	if(forma.email.value=='' && forma.telefon.value=='')
		{
			$('#notice-'+id).text('Morate popuniti bar jedno polje.');
			forma.email.focus();
			return (false);
		} 
	
	$('#'+id).ajaxSubmit(
	{
	    success:  function(t) 
	    {
		    $('#notice-'+id).text(t);
  		}
	}
	);
}


function closeContact(id, path)
{
	$('#premium-'+id).load(path + 'deamon.php?show=JUST_ITEM');
}


function weblink(id, path)
{
	$.ajax({
   	type: "POST",
   	url: path + 'deamon.php',
   	data: "id="+id
    });
 return false;
}

// Popup Google Maps
function gMaps(lat, lon, title)
{
	var posX = screen.availWidth/2 - 200;
	var posY = screen.availHeight/2 - 200;
	window.open('http://www.indeks.ba/map_page.php?lat='+lat+'&lon='+lon+'&title='+title,'Lokacija',
	'width=400,height=400,resizable=0,screenX='+posX+',screenY='+posY);
}
