/**
 * 
 * @param {String} ini Un sir cu valoarea initiala a input-ului
 * @return {Object}
 */
jQuery.fn.inputInit = function(ini){
	return this.each(function(){
		var val = $(this).attr('value');
		val = ( val == '' ) ? ini : val ;
		$(this).attr('value', val);
		$(this).click(function(){
			if ( $(this).attr('value') == ini ) {
				$(this).attr("value", '');
			}
		}, [ini]).blur(function(){
			if ($(this).attr('value') == '') {
				$(this).attr('value', ini);
			}
		}, [ini]);
	}, [ini] );
}
var defaultTextTrusa;
var customTextTrusa;
function updateTrusa(){
	var tools = $.cookie('trusa_cu_unelte');
	if (tools) {
		tools = tools.split('|').length;
		if (tools == 1) {
			customTextTrusa = 'Aveti un produs in trusa cu unelte!';
			$('a#trusa').text(customTextTrusa);
		} else if (tools > 1) {
			customTextTrusa = 'Aveti ' + tools + ' produse in trusa cu unelte!';
			$('a#trusa').text(customTextTrusa);
		} 
	}else {
		$('a#trusa').text(defaultTextTrusa);
	}
}

$(document).ready(function(){
	
	$('.lightbox').lightBox();
	
  if ($.browser.msie && $.browser.version == 6.0){
		$(".img_produs img").pngfix();
	}
	
	$("#ajutor_trusa, #trimite_unui_prieten, A[rel~=popin]").prettyPopin();
	$("textarea").css({resize:"none",overflow:"auto"});
	
	$('#adresa').inputInit('E-mail:');
	$(".cauta_produse").inputInit('Cauta produsul dorit:');
	$(".cauta_articole").inputInit('Cautare:');
	

	$("#faq li p").css({display:"none"});
	$("#faq li a").click(function(){
		$(this).next().slideToggle("fast");
	});
	$("#detalii_produs table").attr({
		id: "date_tehnice",
		width: "100%",
		cellPadding: "0",
		cellSpacing: "0"
	});
	$("#detalii_produs tr:odd").addClass("impar");
	$("#detalii_produs td:first-child").each(function(){
		$(this).after('<th scope="col">' + this.firstChild.nodeValue + '</th>').remove();
	});
	
	$('.adauga_in_trusa').click(function(event){
		event.preventDefault();
		var id = this.rel.replace('popin ', '');
		var trusa = $.cookie('trusa_cu_unelte');
		if (trusa) {
			if (trusa.indexOf(id) == -1) {
				if (trusa.length > 0) {
					trusa += '|' + id;
				}
				else {
					trusa = id;
				}
			}
		} else {
			trusa = id;
		}
		$.cookie('trusa_cu_unelte', trusa, {expire:7});

		updateTrusa();

	});
	
	$('.sterge_din_trusa').unbind().click(function(event){
		event.preventDefault();
		var id = this.rel.replace('popin ', '');
		var trusa = $.cookie('trusa_cu_unelte');
		if (trusa) {
			if (trusa == id) {
				trusa = null;
			}
			else 
				if (trusa.indexOf(id) == 0) {
					trusa = trusa.replace(id + '|', '');
				}
				else {
					trusa = trusa.replace('|' + id, '');
				}
		} else {
			trusa = null;
		}
		$.cookie('trusa_cu_unelte', trusa, {expire:7});
		updateTrusa();

		if ( trusa == null ) {
			$('#continut_trusa_cu_unelte').fadeOut('slow', function(){
				$('.mesaj_trusa_goala').fadeIn('slow');
			});
		} else {
			$(this).parent().parent().fadeOut('slow');
		}

	});
	
	defaultTextTrusa = $('a#trusa').text();
	updateTrusa();
	
	$('a[rel~=external]').unbind().click(function(){
		window.open(this.href, '_blank');
		return false;
	});
});








function showDiv(div){
	document.getElementById(div).style.visibility = 'visible';
}
function hideDiv(div){
	document.getElementById(div).style.visibility = 'hidden';
}










function getElementsByClass (searchClass) {  // This function returns an array of all HTML objects with the  // specified className.  Tag is optional     var returnArray = [];  var els = document.getElementsByTagName('*');  var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');  for (var i = 0; i < els.length; i++) {    if ( pattern.test(els[i].className) ) { returnArray.push(els[i]); }  }  return returnArray;}function popVideo(vid, darken) {  // This function accepts a division ID (vid), either a string or the actual  // object itself.   vid is mandatory.   darken is optional, if it's true  // the page will be greyed out under the video.  var videos = getElementsByClass('video');     // Get all the videos on the page.  var isplaying=null;  for(i=0; i<videos.length; i++) {              // Loop through all the videos    if (videos[i].style.display=='block') {     // This video is playing       isplaying=videos[i].id;                   // remember its name      var tmp=videos[i].innerHTML;              // Get the division contents      videos[i].innerHTML='';                   // destroy the contents      videos[i].style.display='none';           // Terminate the video.      videos[i].innerHTML=tmp;                  // rebuild the contents.    }  }  // This handles the darkening of the background while a video is playing.  // First we see if the dark layer exists.  var dark=document.getElementById('darkenScreenObject');  if (!dark) {    // The dark layer doesn't exist, it's never been created.  So we'll    // create it here and apply some basic styles.    var tbody = document.getElementsByTagName("body")[0];    var tnode = document.createElement('div');          // Create the layer.        tnode.style.backgroundColor='rgb(0, 0, 0)';     // Make it black.        tnode.style.opacity='0.7';                      // Set the opacity (firefox/Opera)        tnode.style.MozOpacity='0.70';                  // Firefox 1.5        tnode.style.filter='alpha(opacity=70)';         // IE.        tnode.style.zIndex='1';                         // Zindex of 50 so it "floats"        tnode.style.position='absolute';                // Position absolutely        tnode.style.top='0px';                          // In the top        tnode.style.left='0px';                         // Left corner of the page        tnode.style.overflow='hidden';                  // Try to avoid making scroll bars                    tnode.style.display='none';                     // Start out Hidden        tnode.id='darkenScreenObject';                  // Name it so we can find it later    tbody.appendChild(tnode);                           // Add it to the web page    dark=document.getElementById('darkenScreenObject'); // Get the object.  }  dark.style.display='none';  if ((isplaying==vid)||(/^close$/i.test(vid))) { return false; }  if (typeof(vid)=="string") { vid=document.getElementById(vid); }  if (vid&&typeof(vid)=="object") {    if (darken) {      // Calculate the page width and height       if( window.innerHeight && window.scrollMaxY )  {         var pageWidth = window.innerWidth + window.scrollMaxX;        var pageHeight = window.innerHeight + window.scrollMaxY;      } else if( document.body.scrollHeight > document.body.offsetHeight ) {        var pageWidth = document.body.scrollWidth;        var pageHeight = document.body.scrollHeight;      } else {         var pageWidth = document.body.offsetWidth + document.body.offsetLeft;         var pageHeight = document.body.offsetHeight + document.body.offsetTop;       }      //set the shader to cover the entire page and make it visible.       dark.style.width= pageWidth+'px';      dark.style.height= pageHeight+'px';      dark.style.display='block';                                    }    // Make the video visible and set the zindex so its on top of everything else    vid.style.zIndex='100';        vid.style.display='block';    var scrollTop = 0;    if (document.documentElement && document.documentElement.scrollTop)      scrollTop = document.documentElement.scrollTop;    else if (document.body)      scrollTop = document.body.scrollTop;    // set the starting x and y position of the video    vid.style.top=scrollTop+Math.floor((document.documentElement.clientHeight/2)-(vid.offsetHeight/2))+'px';    vid.style.left=Math.floor((document.documentElement.clientWidth/2)-(vid.offsetWidth/2))+'px';  }  return false;}
