/**
 * Obter Base Url
 */
function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var index = url.indexOf('/', 14);
    var baseURL = url.substring(0, index);


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

function notify(message, cls){
	var box = '<div class="notification '+cls+' png_bg">'+
		'<a href="#" class="close"><img src="'+getBaseURL()+'themes/default/assets/images/icons/cross_grey_small.png" title="Fechar notificação!" alt="Fechar" /></a>'+
		'<div>'+message+'</div>'+
	'</div>';
	return box;
}

function iframe() 
{
	if($('#catalogo')){
		$('#catalogo').remove();
	}
	
	var el = document.createElement("iframe");
	el.setAttribute('id', 'catalogo');
	el.setAttribute('src', 'http://www.ambientes.pt/catalogo/ambientes');
	el.setAttribute('scrolling','no');
	el.setAttribute('frameborder', '0');
	
	$('#iframe-stage').append(el);	
}

function reload_iframe(){
	//document.getElementById('catalogo').contentWindow.location.reload(true);
	document.getElementById('catalogo').src=document.getElementById('catalogo').src; 
}

function get_folio(id){
	$('.panel-folio').html('');
	
	if(id){
		$('.panel-folio').portfolioSetter({xmlSource:getBaseURL()+'pt/portfolio/index/'+id});
	} else {
		$('.panel-folio').portfolioSetter({xmlSource:getBaseURL()+'pt/portfolio/index'});
	}
	
}

var vilarte = {
	onReady : function() {
		
		
		$("body").delegate("div#top-body a#bt_brindes",'click',vilarte.catalogue);
		//$("body").delegate("div#top-body a#bt_acerca",'click',vilarte.page_acerca);
		$("body").delegate("div#top-body a#bt_contacto",'click',vilarte.page_contact);
		
		$("body").delegate("div.painel-catalogo a#back",'click',vilarte.catalogue_back);
		$("body").delegate("div.painel-catalogo a#reload",'click',vilarte.catalogue_reload);
	},
	fonts : function(e){
		WebFontConfig = {
			    google: { families: [ 'Varela+Round:latin', 'Varela:latin', 'Muli:latin' ] }
			  };
			  (function() {
			    var wf = document.createElement('script');
			    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
			      '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
			    wf.type = 'text/javascript';
			    wf.async = 'true';
			    var s = document.getElementsByTagName('script')[0];
			    s.parentNode.insertBefore(wf, s);
		})(); 
	},
	catalogue: function (e){
		iframe();
		$.scrollTo('div.painel-catalogo', 800);
	},
	catalogue_back: function(e){
		$.scrollTo('div.painel-window', 800);
	},
	catalogue_reload: function(e){
		reload_iframe();
	},
	page_acerca: function(e){
		e.preventDefault();
		
		$.facebox.settings.lateral = false;
		$.facebox({ ajax: getBaseURL()+"pt/pages/acerca"});
	},
	page_contact: function(e){
		e.preventDefault();
		
		$.facebox.settings.lateral = false;
		$.facebox({ ajax: getBaseURL()+"pt/pages/contacto"});
	},
	closeNotification : function(e){
		e.preventDefault();
		
		$(this).parent().fadeTo(400, 0, function () { // Links with the class "close" will close parent
			$(this).slideUp(400);
		});
		return false;
	}
};

$(document).ready(
	vilarte.onReady	
);

