/* init login popup for admins */
$(function() {$('.adm').boxy();});

/* dropdown select box on home search */
$(document).ready(function() {
	$('#search_area_main').selectbox();
	$('#search_trade_main').selectbox();
});

/* input boxes on home/signup page */
function setClass(theid, theclass) {
	document.getElementById(theid).className = ''+theclass;
}
function inputCheckClass(theid, theclass) {
	if(document.getElementById(theid).value == "") {
		setClass(theid, theclass);
	}
}

/* textarea resize on trader contact form */
$(document).ready(function() {
	$('textarea.resizable:not(.processed)').TextAreaResizer();
	$('iframe.resizable:not(.processed)').TextAreaResizer();
});


/* profile work image popup */
//product image popup
$(function() {
	$('.work').lightBox({fixedNavigation:true});
});

/* rating system on profile */
function updateOverall() {
	var rate1 = parseInt($('#rating_1').val());
	var rate2 = parseInt($('#rating_2').val());
	var rate3 = parseInt($('#rating_3').val());
	var rate4 = parseInt($('#rating_4').val());
	var rate5 = parseInt($('#rating_5').val());
	var new_total = rate1 + rate2 + rate3 + rate4 + rate5;
	$('#rating_6').val(new_total); //set new total
	
	var star_rating = Math.round(((new_total / 5) / 2) / 10);
	if(star_rating == 1) {
		$('#preview_stars').css({ width: 26 });
		$('#preview_stars').removeClass('red');
		$('#preview_stars').removeClass('green');
		$('#preview_stars').addClass('star-num red');
	} else if(star_rating == 2) {
		$('#preview_stars').css({ width: 52 });
		$('#preview_stars').removeClass('red');
		$('#preview_stars').removeClass('green');
		$('#preview_stars').addClass('star-num red');
	} else if(star_rating == 3) {
		$('#preview_stars').css({ width: 78 });
		$('#preview_stars').removeClass('red');
		$('#preview_stars').removeClass('green');
		$('#preview_stars').addClass('star-num');
	} else if(star_rating == 4) {
		$('#preview_stars').css({ width: 104 });
		$('#preview_stars').removeClass('red');
		$('#preview_stars').removeClass('green');
		$('#preview_stars').addClass('star-num green');
	} else if(star_rating == 5) {
		$('#preview_stars').css({ width: 130 });
		$('#preview_stars').removeClass('red');
		$('#preview_stars').removeClass('green');
		$('#preview_stars').addClass('star-num green');
	} else {
		$('#preview_stars').css({ width: 26 });
		$('#preview_stars').removeClass('red');
		$('#preview_stars').removeClass('green');
		$('#preview_stars').addClass('star-num red');
	}
	return false;
}
function updateRating(id, val) {
	$('#rating_'+id).val(val);
}

/* toggle searching options */
jQuery.fn.fadeToggle = function(s, fn){
     return (this.is(":visible"))
         ? this.fadeOut(s, fn)
         : this.fadeIn(s, fn);
};
function switchSearch() {
	if($('#search-bytrade').css('display') == 'block') {
		$('#search_companyname').val('');
		$('#search_trade').val('');
		$('#search_area').val('');
		$('#search-bytrade').fadeToggle('', function() {$('#search-byname').fadeToggle();});
	} else {
		$('#search_companyname').val('');
		$('#search-byname').fadeToggle('', function() {$('#search-bytrade').fadeToggle();});
	}
	return false;
}

/* toggle switch login/forgot pass on home */
function switchCommand() {
	if($('#main-login').css('display') == 'block') {
		$('#main-login').fadeToggle('', function() {$('#main-forgotpass').fadeToggle();});
	} else {
		$('#main-forgotpass').fadeToggle('', function() {$('#main-login').fadeToggle();});
	}
	return false;
}


/* jquery beaty tips */
$(document).ready(function() {
	$('.general-help').bt({
	  trigger: ['focus','blur'],
	  padding: 14,
	  spikeLength: 12,
	  spikeGirth: 8,
	  cornerRadius: 10,
	  overlap: -5,
	  overlay: false,
	  fill: 'rgba(0, 0, 0, .8)',
	  strokeWidth: 2,
	  strokeStyle: '#02829f',
	  animate: true,
	  cssStyles: {width: 'auto', color: '#fff', fontWeight: 'normal', fontFamily: 'Verdana, Arial, Helvetica', fontSize: '12px', textAlign: 'center'},
	  textzIndex: 9999,
	  boxzIndex: 9990, 
	  positions: ['left']
	});
	$('.general-tip').bt({
	  trigger: 'hover',
	  padding: 14,
	  spikeLength: 12,
  	  spikeGirth: 8,
	  cornerRadius: 10,
	  overlap: -5,
	  fill: 'rgba(0, 0, 0, .8)',
	  strokeWidth: 2,
	  strokeStyle: '#02829f',
	  distance: 20,
	  animate: true,
	  cssStyles: {width: 'auto', color: '#fff', fontWeight: 'normal', fontFamily: 'Verdana, Arial, Helvetica', fontSize: '12px', textAlign: 'center'},
	  positions: ['left']
	});
	$('.general-tip-low').bt({
	  trigger: 'hover',
	  padding: 10,
	  spikeLength: 12,
  	  spikeGirth: 8,
	  cornerRadius: 10,
	  fill: 'rgba(0, 0, 0, .8)',
	  strokeWidth: 2,
	  strokeStyle: '#02829f',
	  cssStyles: {width: 'auto', color: '#fff', fontWeight: 'normal', fontFamily: 'Verdana, Arial, Helvetica', fontSize: '12px', textAlign: 'center'},
	  positions: ['bottom']
	});
});


/* popup window */
function popitup(url, name, wid, hei) {
	newwindow=window.open(url,name,'height='+hei+',width='+wid+', scrollbars=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}