// Vladimir Mitic, ChannelNet, 2009 
 	

         jQuery.fn.cpphoverbuttonsold=function() {
                $(this).hover(function() {var img = $(this).css("background-image"); 
                                          $(this).css("background-image",img.replace(".jpg","-over.jpg").replace(".gif","-over.gif").replace(".png","-over.png"));   },
                              function() {var img = $(this).css("background-image"); 
                                          $(this).css("background-image",img.replace("-over.jpg",".jpg").replace("-over.gif",".gif").replace("-over.png",".png"));   }

                );                
          }
          
         jQuery.fn.cpphoverbuttons=function() {
                $(this).hover(function() { $(this).css("background-position","-500px 0");   },
                              function() {  $(this).css("background-position","0 0"); }

                );                
          }          

         jQuery.fn.cpptabsmenu=function() {
            /* css should already makes all tabs disabled*/
            /* add on click events to tabs */
            $(this).find(".tab").each(function() {
                  /* vertically allign text withing the tab */
                  if($(this).find("span").text().length>14)  $(this).find("span").css("margin-top","14px");
                  else $(this).find("span").css("margin-top","21px");
                  $(this).find("span").fadeIn("slow");
                  /* add on click event */
                  $(this).click(function(){
                        /* make all tabs off first*/
                        $(".tab").each(function(){ 
                            var img=$(this).css("background-image");
                            if (img.indexOf("-on.gif")>-1)$(this).css("background-image",img.replace("-on.gif","-off.gif")); 
                            $(this).find("span").removeClass("ttactive");
                        });
                        /* hide all content */
                        $(".tabcntnt:visible").fadeOut("fast");
                        /* make curent tab on */
                        var img=$(this).css("background-image");
                        if (img.indexOf("-off.gif")>-1) $(this).css("background-image",img.replace("-off.gif","-on.gif"));
                        var tabid=$(this).attr("id").replace("tab","content");
                        $(this).find("span").addClass("ttactive");
                        /* show current content */
                        $("#"+tabid).fadeIn("slow");                        
                        /* remove selection mark on some browsers */
                        $(this).blur();                        
                  });              
            });
            $(this).find(".tab:first").click(); /* enable first tab */
        }

        jQuery.fn.cppdropdownmenu=function() {
            $(this).click(function(){$("#menutop .topdropdown").slideToggle("medium");});
            $("#menutop .topdropdown li:last").css("border","none");            
        }

        jQuery.fn.cppinputtext=function() {
            $(this).each(function(){
				var defVal = $(this)[0].defaultValue;
                $(this).data("empty",defVal);            
	            /*
                $(this).data("empty",$(this).val());            
                */
                $(this).focus(function(){if($(this).val()==$(this).data("empty"))$(this).val(""); $(this).addClass("changed");});
                $(this).blur(function(){if($(this).val()=="")$(this).removeClass("changed").val($(this).data("empty"));else $(this).addClass("changed");});
            });
        }
        
        jQuery.fn.cpppassword=function() {            
            $(this).each(function(){                                            
                $(this).focus(function(){$(this).parent().find(".maskedtextboxemptyvalue").css("display","none");});
                $(this).blur(function(){if($(this).val()=="")$(this).parent().find(".maskedtextboxemptyvalue").css("display","block");else $(this).parent().find(".maskedtextboxemptyvalue").css("display","none");});
            });
        }        

        jQuery.fn.cppradio=function() {
            $(this).each(function(){
                $(this).click(function() {
                    var group = $(this).attr("name");
                    var value = $(this).attr("value");
                    $(".cppradio").each(function() {   /* disselect all first  */                                                                                
                        if ($(this).attr("name") == group)
                            $(this).parent().find("span").removeClass("changed");
                    });                                                           
                    $(this).parent().find("span").addClass("changed");
                    $(this).blur();
                });                
            });
        }

        jQuery.fn.cppsubmit=function() {
            /* save default values */
            $(this).click(function(){
                var errors = 0;
                $("#form_error_fields").empty();
                /*------------- check required fields */
                $(".required:visible").each(function(){                    
                    if ($(this).val()==$(this).data("empty"))
                    {
                        $("#form_error_fields").append("-" + $(this).parent().find("span").text() + " " + $("#isrequiredfield").val() + "<br\>");                        
                        errors++;
                    }
                });                
                $("select.required").each(function(){                    
                    if ($(this).val()==$(this).data("empty"))
                    {
                        $("#form_error_fields").append("-" + $(this).parent().parent().parent().find(".label").text() + " " + $("#isrequiredfield").val() + "<br\>");                        
                        errors++;
                    }
                });
            	/*------------- check valid fields */
                $(".validate:visible").each(function(){
                    if($(this).val()!=$(this).data("empty"))
                    {
                		var validationID = "#"+$(this).attr("id")+"_val";
                		/* validate max length */
                		if (($(validationID) != null) && ($(validationID).val().indexOf("MAX_LENGTH:")!= -1))
                        {
                			var maxlength = $(validationID).val().replace("MAX_LENGTH:","");
                            if($(this).val().length > maxlength)
                            {
                                $("#form_error_fields").append("-" + $(this).parent().find("span").text() + " " + $("#isnotvalidfield").val() + " ("+$("#limitof").val()+" "+maxlength+")<br\>");
                                errors++;
                            }
                        } else /* validate other using regular expression */
                        if(($(validationID) != null) && !($(this).val().match(new RegExp($(validationID).val()))))
                        {
                            $("#form_error_fields").append("-" + $(this).parent().find("span").text() + " " + $("#isnotvalidfield").val() + "<br\>" );
                            errors++;
                        }
                    }                                
                });                
                //$("#form_error_fields").append(" " + errors + "<br\>");
                if(errors == 1) alert($("#pleasereview1").val());
                if(errors > 1) alert($("#pleasereviewmore").val());                
                if(errors == 0) {
                    /* clear captions before submiting the form! */                    
                    $(".cpptextboxl:visible, .cpptextarea:visible").each(function(){if($(this).val()==$(this).data("empty"))$(this).val("");});
                    Submit();           /* here call page custom function: Submit(); */                    
                }               
            });
        }



        jQuery.fn.constrain_size = function(maxwidth, maxheight, align) {
            this.each(function() {      
                if (align) $(this).css("margin","0");
                $(this).bind('load',function(){
                    var height = $(this).height();
                    var width = $(this).width();
                    //if (height == 0 || width == 0) {$(this).css("display","block"); return; }
                    var ratio = 1.0;
                    if (height > 0 && width > 0) ratio = height / width;
                    if (width > maxwidth) {
                        $(this).width(maxwidth + "px");
                        height = (ratio * maxwidth);
                        $(this).height(height + "px");
                    }                                                                
                    if (height > maxheight) {
                        $(this).height(maxheight + "px");
                        width = (maxheight / ratio);
                        $(this).width(width + "px");
                    }                                                                    
                    if (align) {    
                        $(this).css("float","left");
                        var offset = maxwidth - width;
                        if(offset>0) $(this).css("margin-left", Math.floor(offset/2)+"px");     /*do horizontal align */
                        offset = maxheight - height;
                        if(offset>0) $(this).css("margin-top", Math.floor(offset/2)+"px");  /* do vertical align */
                    }
                    $(this).fadeIn("slow");//.css("display","block");
                }).each(function(){ //needed for potential cached images!!! (if loaded already will not fire event)
                        if (this.complete || this.complete === undefined){ this.src = this.src; } 
                });
            });
        }
        
        
        jQuery.fn.cppgalleryLoad=function() {
           //if(window.currentIndex === undefined) return; /* prevent running this on the wrong page since we are not using any binding $(this).each(...) */
           $(this).each(function() {
               if(currentIndex == 0) $('#prev').css("display","none"); else $('#prev').css("display","block");
               if(imgSet.length > (currentIndex + 4)) $('#next').css("display","block"); else $('#next').css("display","none");
               
               if(imgSet.length > currentIndex)
                    $(".imagebox:eq(0) img").css("width","").css("height","").css("display","none").attr("src",imgSet[currentIndex].image).attr("alt",imgSet[currentIndex].alt).constrain_size(275,160,true);
               else $(".imagebox:eq(0) img").css("display","none");
               if(imgSet.length > currentIndex+1)
                    $(".imagebox:eq(1) img").css("width","").css("height","").css("display","none").attr("src",imgSet[currentIndex+1].image).attr("alt",imgSet[currentIndex+1].alt).constrain_size(275,160,true);
               else $(".imagebox:eq(1) img").css("display","none");
               if(imgSet.length > currentIndex+2)
                    $(".imagebox:eq(2) img").css("width","").css("height","").css("display","none").attr("src",imgSet[currentIndex+2].image).attr("alt",imgSet[currentIndex+2].alt).constrain_size(275,160,true);
               else $(".imagebox:eq(2) img").css("display","none");
               if(imgSet.length > currentIndex+3)
                    $(".imagebox:eq(3) img").css("width","").css("height","").css("display","none").attr("src",imgSet[currentIndex+3].image).attr("alt",imgSet[currentIndex+3].alt).constrain_size(275,160,true);             
               else $(".imagebox:eq(3) img").css("display","none");
           });
        }
        
        jQuery.fn.cppgalleryPrev=function() {  
                $(this).click(function(){                      
                    currentIndex = currentIndex - 4;
                    if (currentIndex < 0) currentIndex = 0; 
                    $("#galleryimages").cppgalleryLoad();                
                 });
        }
        
        jQuery.fn.cppgalleryNext=function() {
                $(this).click(function(){                        
                    currentIndex = currentIndex + 4;
                    if ((currentIndex) >= imgSet.length) currentIndex = currentIndex - 4;
                    $("#galleryimages").cppgalleryLoad();                                
                });
        }        

        jQuery.fn.filterresults=function() {            
            $(this).each(function(){    
                    var srchstr = $(this).val().toLowerCase();
                    $(".searchresults").each(function() {
                        if($(this).text().toLowerCase().indexOf(srchstr) >= 0) 
                            $(this).css("display","inline"); //.fadeIn('slow');
                    });
            });
        } 

        $(document).ready(function(){ 
            $(".fixpng").ifixpng(false);                                                /* fixes png */            
            $(document).cpptabsmenu();                                                  /* tabs menu */            
            $(".rollover").cpphoverbuttons();          				                    /* hover buttons */
            $(".topmenu").cppdropdownmenu();                                            /* dropdown menu */            
            $(".cppselectbox").selectbox();                                             /* customise drop down box for forms */
            //$(".selectbox-wrapper").bgiframe();                                       /* prevent control bleeding through */
            $(".cpptextbox,.cpptextboxl,.cpptextarea,.cppselectbox,.projectinput,input.email").cppinputtext();    /* set behaviour for text boxes */
            $(".maskedtextbox").cpppassword();                                          /* set behaviour for password boxes */
            $(".cppradio").cppradio();                                                  /* set behaviour for radio buttons */
            $("a,object").click(function(){$(this).blur();});                           /* avoid showing doted lines on the link/flash after click  (used css too)*/
            $("#cu_submit").cppsubmit();                                                /* submit form behaviour */                        
            
            /* gallery functionality */
            $("#galleryimages").cppgalleryLoad();
            $("#prev").cppgalleryPrev();
            $("#next").cppgalleryNext();   
            
            /* schedule an estimate functionality */         
            $("#postalcodeprefill").each(function(){$("#postal").val($(this).val()).addClass("changed");});
            /* do not strech bio image, it looks ugly, fit it in the box instead */
            $(".ourpeoplebioimage").constrain_size(108, 108, false); 
            /* do not show results that do not match search string */
            $("#searchresults").filterresults();                    
        });
