/* General */
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));
};

$j(document).ready(set_poliseos);

$j(window).resize(set_poliseos);


function go_to_url(url){
	document.location.href= url;
}

/* Top menu */
function set_topmenu_hover(itemId){
	document.getElementById('topmenu-item-left-'+itemId).style.background = 'url(' + pathRel + 'img/topmenu-item-left-hover.jpg)  0 0 no-repeat #533768';
	document.getElementById('topmenu-item-center-'+itemId).style.background = 'url(' + pathRel + 'img/topmenu-item-center-hover.jpg) 0 0 repeat-x #533768';
	document.getElementById('topmenu-item-right-'+itemId).style.background = 'url(' + pathRel + 'img/topmenu-item-right-hover.jpg) 0 0 no-repeat #533768';
}


function unset_topmenu_hover(itemId){
	document.getElementById('topmenu-item-left-'+itemId).style.background = 'none';
	document.getElementById('topmenu-item-center-'+itemId).style.background = 'none';
	
	if(itemId=='last'){
		document.getElementById('topmenu-item-right-'+itemId).style.background = 'none';
	}else{
		document.getElementById('topmenu-item-right-'+itemId).style.background = 'url(' + pathRel + 'img/topmenu-item-right.jpg) 0 0 no-repeat #533768';
	}
}


function set_form_step_hover(itemId){
	document.getElementById('form-step-'+itemId).style.color = '#ed9b11';
	document.getElementById('form-step-left-'+itemId).style.background = 'url(img/form-step-left.png) no-repeat 0 -29px';
	document.getElementById('form-step-center-'+itemId).style.background = 'url(img/form-step-center.png) repeat-x 0 -29px';
	document.getElementById('form-step-right-'+itemId).style.background = 'url(img/form-step-right.png) no-repeat 0 -29px';	
}

function unset_form_step_hover(itemId){
	document.getElementById('form-step-'+itemId).style.color = '#fac263';
	document.getElementById('form-step-left-'+itemId).style.background = 'url(img/form-step-left.png) no-repeat 0 0';
	document.getElementById('form-step-center-'+itemId).style.background = 'url(img/form-step-center.png) repeat-x 0 0';
	document.getElementById('form-step-right-'+itemId).style.background = 'url(img/form-step-right.png) no-repeat 0 0';	
}


function get_all_width(){	
	var theWidth;
	// Window dimensions:
	if (window.innerWidth) {
		theWidth=window.innerWidth;
	}
	else if (document.documentElement.clientWidth) {
		theWidth=document.documentElement.clientWidth;
	}
	else if (document.body) {
		theWidth=document.body.clientWidth;
	}
	return theWidth;
}

function get_all_height(){	
	var theHeight;
	// Window dimensions:
	if (window.innerHeight) {
		theHeight=window.innerHeight;
	}
	else if (document.documentElement.clientHeight) {
		theHeight=document.documentElement.clientHeight;
	}
	else if (document.body) {
		theHeight=document.body.clientHeight;
	}
	return theHeight;
}

function set_poliseos(){
	var all_width = get_all_width();
	var poliseos_left = ((all_width-990)/2);
	document.getElementById('poliseos').style.right = poliseos_left+'px';	
}

function setPaymentMethod(paymentMethod) {
	var paymentMethodId = document.getElementById('paymentForm:' + paymentMethod);
	var paymentMethodControl = document.getElementById('paymentForm:paymentMethod');
	paymentMethodControl.value = paymentMethodId.innerHTML;
	var selectedBox = document.getElementById(paymentMethod);
	selectedBox.checked = true;
}

function togglePanel(show, targetPanel) {
	if (show) 
		$j('#detailsForm\\:'+targetPanel).slideDown('normal');
	else 
		$j('#detailsForm\\:'+targetPanel).slideUp('normal');
}

function deleteItem(index) {
	$j('#detailsForm\\:selected_'+ index).val(false);
	$j('#detailsForm\\:itemName_'+ index).val(null);
	$j('#detailsForm\\:itemYear_'+ index).val(null);
	$j('#detailsForm\\:itemValue_'+ index).val(null);
	togglePanel(false, 'contentItem_'+index);
}

function addItem() {
	var firstNotActive = null, lastActive = null;
	$j.each($j('#detailsForm\\:contentItems > span'), function() {
		var elem = $j(this);
		var selected = elem.find('input[type=hidden]');
		var value = selected.val();
		if (value == "false" && firstNotActive == null)
			firstNotActive = elem;
		else if (value == "true")
			lastActive = elem;
	});
	
	if (firstNotActive != null) {
		if (lastActive != null) {
			lastActive.after(firstNotActive);
		}
	
		firstNotActive.find('input[type=hidden]').val(true);
		firstNotActive.slideDown('normal');
	}
}

function isDayEnabled(day){
	var curDt = new Date();
	var toDate = new Date();
	toDate.setDate(toDate.getDate() + new Number($j('#advanceSalesDays').text()));
    if (curDt.getTime() - day.date.getTime() < 0 &&
    		toDate.getTime() - day.date.getTime() > 0) 
    	return true;
    else
    	return false;
}

function showHideClaimlessYears(init) {
	var claimsLastYearInput = $j('#insuranceFormBase\\:claimsLastYear');
	var claimsLastYear = claimsLastYearInput.val();
	var previousInsurer = $j('#insuranceFormBase\\:previousInsurer').val();
	var claimlessYearsElem = $j('#insuranceFormBase\\:claimlessYears').parents('.form-item-dark');
	var claimsLastYearElem = $j('#insuranceFormBase\\:claimsLastYear').parents('.form-item-dark');
	if (previousInsurer) {
		if (previousInsurer == 'pl.poliseo.backend.common.model.InsuranceCompany:5') {
			claimsLastYearElem.show();
			if (claimsLastYear && claimsLastYear == 0) {
				claimlessYearsElem.show();
			} else {
				claimlessYearsElem.hide();
			}
		} else if (previousInsurer == 'pl.poliseo.backend.common.model.InsuranceCompany:7') {
			claimsLastYearElem.hide();
			claimlessYearsElem.hide();
			if (!init)
				claimsLastYearInput.val('');
		} else {
			claimsLastYearElem.hide();
			claimlessYearsElem.show();
			if (!init)
				claimsLastYearInput.val('');
		}
	} else {
		claimsLastYearElem.hide();
		claimlessYearsElem.hide();
		if (!init)
			claimsLastYearInput.val('');
	}
	
	if (!init)
		$j('#insuranceFormBase\\:claimlessYears').val('');
}

$j(document).ready(function() {
	$j('input.integer').numeric();
	$j('input.decimal').numeric({allow: ' '});
	$j('input.float').numeric({allow: ',. '});
	
	$j('input.decimal').blur(function() {
		this.value = this.value.split(' ').join('');
	});
	$j('input.float').blur(function() {
		this.value = this.value.split('.').join(',');
		this.value = this.value.split(' ').join('');
	});
	$j('input.trimmed').blur(function() {
		this.value = $j.trim(this.value);
	});
	$j('input.uppercase').blur(function() {
		if (this.value.length > 0)
			this.value = this.value.charAt(0).toUpperCase() + (this.value.length > 1 ? this.value.substring(1) : '');  
	});
});

$j(document).ready(function() {
	$j("input[title]").tooltip({
		position: "bottom right",
		offset: [-40, 50],
		effect: "fade",
		tipClass: "form-tooltip"
	}).dynamic({ bottom: { offset: [-40, 50] } });
	
	$j(".tip[title]").tooltip({
		position: "bottom right",
		offset: [-2, 5],
		effect: "fade"
	}).dynamic({ bottom: { offset: [-40, 50] } });
});
