function login_facebook() { window.location.reload(true); }

function logout_facebook() { window.location.reload(true); }

function get_publi(data) {
    //url,tipus,nom,lloc)
    if(data) {
        url = data[0];
        tipus = data[1];
        nom = data[2];
        lloc = data[3];

        jQuery('#'+lloc).load(url,'publi='+tipus+'&name='+nom);

    }
}

function get_track_publi(data) {
    if(data) {
        url = data[0];
        tipus = data[1];
        nom = data[2];
        lloc = data[3];
	track_id = data[4];

        jQuery('#'+lloc).load(url,'publi='+tipus+'&name='+nom,function(){
		params = { track: track_id };
		jQuery('#'+lloc+' a').attr("target", "_blank");
		jQuery('#'+lloc+' a').click(function(){
			jQuery.get(url,params);
			return true;
		})
	});
    }
}

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');

        return true;
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

jQuery(document).ready(function(){
    jQuery('a:external').attr("target", "_blank");
    // xhtml 1.0 strict way of using target _blank
    
    // autocentering
    var web20FullWidth=jQuery('.web20').width();
    var web20Width=jQuery('.web20:first ul.socials li').width();
    var web20Count=jQuery('.web20:first ul.socials li').length;
    var numPerRow=Math.floor(web20FullWidth/web20Width);
    var web20RowWidth=Math.min(numPerRow, web20Count)*web20Width;
    var web20LeftMargin=(web20FullWidth-web20RowWidth)/2;
    jQuery('.web20').css('margin-left', web20LeftMargin+'px');

    jQuery('.yt_mini').each(function(){youtube(this,300,250)})

    if(typeof(publi_data)!="undefined")
        get_publi(publi_data);

    if(typeof(publi_track_data)!="undefined")
	get_track_publi(publi_track_data);

    jQuery('.content_noticia a[href$="jpg"]').each( function(){
	jQuery(this).addClass('thickbox');
	// provar de posar la imatge i el div.lupa dins de un altre div
	// després, div.lupa amb position-relative
	/*
	jQuery(this).html('<div class="lupa"></div>'+jQuery(this).html());
	jQuery(this).hover(function(){ 
        	jQuery(this).children("div").fadeIn(600); 
	    },function(){ 
        	jQuery(this).children("div").fadeOut(200); 
    	});*/
    });
    jQuery('.content_noticia a[href$="png"]').each( function(){
	jQuery(this).addClass('thickbox');
	/*jQuery(this).append('<div class="lupa"></div>');
	jQuery(this).hover(function(){ 
        	jQuery(this).children("div").fadeIn(600); 
	    },function(){ 
        	jQuery(this).children("div").fadeOut(200); 
    	});*/
    } );

    if(jQuery('#carburants_link').size()) {
	jQuery('#carburants_link').click(function(){
		jQuery('#oratge').hide();
		jQuery('#oratge_link').css('color','#aaa');
		jQuery('#carburants_link').css('color','#000');
		jQuery('#carburants').fadeIn();
	});

	jQuery('#oratge_link').click(function(){
		jQuery('#carburants').hide();
		jQuery('#carburants_link').css('color','#aaa');
		jQuery('#oratge_link').css('color','#000');
		jQuery('#oratge').fadeIn();	
	});
    }

    if(jQuery('.gform_body').size()) {
	jQuery("head").append("<link>");
	jQuery("head").children(":last").attr({
      		rel:  "stylesheet",
      		type: "text/css",
      		href: "http://www.infobenissa.cat/wp-content/themes/infobenissa/js/jwysiwyg/jquery.wysiwyg.css"
	});
	jQuery.getScript("http://www.infobenissa.cat/wp-content/themes/infobenissa/js/jwysiwyg/jquery.wysiwyg.js",
		function() {
			jQuery('.gform_body textarea').wysiwyg({controls:{strikeThrough : { visible : false }}});
		})
    }
})

thickboxL10n = {};
thickboxL10n.next = 'Següent &gt;';
thickboxL10n.prev = '&lt; Anterior';
thickboxL10n.prev = 'Imatge';
thickboxL10n.of = 'de';
thickboxL10n.close = 'Tanca';

jQuery.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/)
            && (obj.hostname != location.hostname);
};

function youtube(e,w,h) {

    var v=jQuery(e).html();

    var str  = '<object width="'+w+'" height="'+h+'">';
    str += '<param name="movie" value="'+v+'"></param>';
    str += '<param name="allowFullScreen" value="true"></param>';
    str += '<param name="allowscriptaccess" value="always"></param>';
    str += '<param name="wmode" value="transparent"></param>';
    str += '<embed src="'+v+'" type="application/x-shockwave-flash" ';
    str += 'allowscriptaccess="always" wmode="transparent" allowfullscreen="true" width="'+w+'" height="'+h+'"></embed>';
    str += '</object>';

    jQuery(e).html(str);

}
