//POPUP
$(document).ready(function() {
    $("#catalogBox .appl a").click(function(){
        popup($(this));    
        return false;
    });
});


function popup(element){
    $.get(element.attr("rel"),function(data){
        $("body").append('<div id="popup-overlay" title="close"></div><div id="popup">'+data+'</div>');        
        var overlayEl = $("#popup-overlay");
        var popupEl = $("#popup");
        overlayEl.css({ height:$(document).height(), width:$(document).width(), opacity: "0.8" }).show();    
    
        var left = ($(window).width() - (popupEl.width()+20))/2;
        var top = $(window).scrollTop()+($(window).height() - (popupEl.height()+20))/2;
        $(window).resize(function(){
            overlayEl.css({ height:$(document).height(), width:$(document).width(), opacity: "0.8" });
            var left = ($(window).width() - popupEl.width())/2;
            var top = $(window).scrollTop()+50;
            popupEl.css({ left: left, top: top });
        });
        
        overlayEl.click(function(){ closePopup(overlayEl,popupEl); });
        popupEl.find(".close").live("click",function(){ closePopup(overlayEl,popupEl); });
    
        if((top+parseFloat(popupEl.height()))>overlayEl.height()){ overlayEl.height((top+parseFloat(popupEl.height()))+30); }
        popupEl.css({ left: left, top: top }).fadeIn("slow");
        
        popupEl.find("form").submit(function(){ submitForm($(this)); return false; });
    });
}

function closePopup(overlayEl,popupEl){
    popupEl.fadeOut("slow",function(){ $(this).remove(); });
    overlayEl.fadeOut("slow",function(){ $(this).remove(); });
    $(window).unbind("resize");
}

function submitForm(formEl){
    formEl.find(".error").removeClass("error");
    formEl.find("label:has(em)").each(function(){
        if($(this).next("input").val()==""){ $(this).next("input").addClass("error"); }
        if($(this).next("textarea").val()==""){ $(this).next("textarea").addClass("error"); }
    });
    if(formEl.find(".error").length!=0){
        return false;
    } else {
        var postData = formEl.serialize();
        $.get(formEl.attr("action")+"?"+postData, function(data){
            formEl.html($(data).find("form").html());
        },"HTML");
    }    
}

$(document).ready(function() {
	$('#floating-banner-slider .itemWrapper').cycle({
		timeout: 2600,
		speed: 500
	});
});


//HOME PAGE MAIN BANNER
$(document).ready(function() {
  if ( $('.main-banner > div').length > 1 ){
    $('#main-banner-wrapper').append('<div id="banner-selector"></div>');
	}
});

$(window).load(function(){
	$('#main-banner-wrapper .main-banner').cycle({
		fx: 'fade',
		timeout: 3000,
		pause: true,
		pager: $('#banner-selector')
	});
});



