

$(document).ready(function() {
	$("#sitemap_container").click(
        function(){
            $("#sitemap_container").slideUp('slow');
    });
    
    $('.productSingleTlorisi').cycle({
        fx: 'fade',
        speed: 'fast',
        timeout: 3000,
        pager: '.productSingleTlorisiNav',
        // callback fn that creates a thumbnail to use as pager anchor 
        pagerAnchorBuilder: function(idx, slide){
            return '<a href="#" class="sliderDot"></a>';
        }
    });
    
    cycleGallery("product_galery");
    cycleGallery("service_galery");
    $('#contentcorner').corner("13px;cc:#1a1a1a");
    $('#contentcorner_small').corner("13px;cc:#1a1a1a");
    $("#myselectbox_container ul li").bind("click",function(){
        location.href = $('#myselectbox').val();
    });            
    $("#sitemap_container").bind("click",function(){
       $('#sitemap_container').slideUp('slow');
    });       
    $(".fbox").fancybox();  
    
    $("div.product_slide").click(
        function(){
            $(this).siblings("div.product_slide").children("div.product_details").slideUp("slow");
            $(this).children("div.product_details").slideDown("slow");
        }
    ); 
    showTab($(".button_area").children()[0],0);
	
	var list	  = $( '#boxes .latest .box' );
    var firstpageCycleObj = $('#picture_main .img_cycle').cycle({ 
        fx:    'fade',
		pager: '.latest',
		after: function(zeroBasedSlideIndex, slideElement) {
			var pager_index = $( slideElement ).css( 'z-index' );
			$( list ).addClass( 'not' );
			$( list ).eq(Math.abs(pager_index-3)).removeClass( 'not' );
		},
		pagerAnchorBuilder: function(idx, slide){
			return 'div.box:eq(' + idx + ')';
		}
    });
    $('#myselectbox').selectbox();
    	
	$("div.slide:first div.details").show();
	$("div.product_slide:first div.product_details").show();
	//$("div.title_container:first div.product_links").show();//
});











function cycleGallery(divName)
{
    
    $("div."+divName).cycle({ 
    	fx:     'scrollHorz', 
    	prev:   '#cycle_left_'+divName, 
    	next:   '#cycle_right_'+divName,
    	timeout: 0,
    	speed: 2000
    });  
}

function showTab(button,tabIndex)
{            
    $(".button_area").children().removeClass("button_active");
    $(".button_area").children().addClass("button_inactive");
    $(button).removeClass("button_inactive");
    $(button).addClass("button_active");
    // zamenjamo tabe   
    $("#__tabs").find(".product_description").css("display","none");
    $("#___tab" + tabIndex).css("display","block");
    // aktivni knof!
}
/**
 *  @description    :   Function turns particular label into color defined as parameter.
 *  @param label    :   Label to apply color on. Label is refered to as id!
 *  @param color    :   Which color to apply on label
 *
 *  @author         :   Jernej Gololicic
 */
function colorPreceedingLabel(label, color){
	
	$("#"+label).css("color",color);
}
/**
 *  @description    :   Function checks if s is integer.
 *  @param mixed s  :   Mixed value to check for integer type.
 *
 *  @ return        :   Function returns true if parameter s is integer.
 */
function isInteger( s ) {
     return !isNaN( parseInt( s ) );
}

/**
 *  @description    :   Function checks if parameter is valid email address.
 *  @param  mixed email :   Mixed value to check for email pattern.
 *
 *  return          :   Function returns true if input parameter is valid email address
 *  @author         :   Jernej Gololicic
 */
function isEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email
   if(reg.test(address) == false) {
      return false;
   }
   return true;
}


/**
 *  @description    :   Removes leading whitespaces
 *  @param value    :   mixed input string
 *
 *  @return         :   Function returns string without leading whitespaces
 *  @author         :   Jernej Gololicic
 */
function lTrim( value ) {

	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");

}

/**
 *  @description    :   Removes ending whitespaces
 *  @param value    :   mixed input string
 *
 *  @return         :   Function returns string without ending whitespaces
 */
function rTrim( value ) {

	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");

}

/**
 *  @description    :   Removes leading and ending whitespaces
 *  @param value    :   mixed input string
 *
 *  @return         :   Function returns string without leading and ending whitespaces
 *  @author         :   Jernej Gololicic
 */
function trim( value ) {
	return lTrim(rTrim(value));
}       
      
      
function urlencode(str) {
	//return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
	return str.replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40').replace(/&/g,'%26').replace(/^\s+|\s+$/g, '');
}

function urldecode(str) {
	return unescape(str.replace('+', ' '));
}
       

    function sendEventQuestion(){

        
    	var validated 	= true;
    	var lang_id		= $("#lang_id").val();

		fieldNames 	= new Array("name", "email", "summary","address","phone");
		fieldTypes 	= new Array("string", "email", "string", "string", "string");
		required   	= new Array(true, true, true, true, true);
		
		colorTrue   = "black";
		colorFalse  = "red";

		label		= new Array("name_label","email_label","summary_label","address_label","phone_label");
		
		validated &= validateUserForm(fieldNames, fieldTypes, required, label, colorTrue, colorFalse);
      
    	/**
    	 *  Submit
    	 */
    	if (validated) {
    
            var submitString = "ask_us=1";
            
    		for (var i in fieldNames) {
    		    // check if there are many fields with this field name
    		    if ($("input[name="+fieldNames[i]+"]").length > 1) {
    		    
    		        $("input[name="+fieldNames[i]+"]").each(function(i){
    		            submitString += "&" + $(this).attr("id") + "=" + urlencode($(this).val());
    				});
    
    			}
    			/**
    			 * Select boxes for person titles
    			 */                   			
                else if ($("select[name="+fieldNames[i]+"]").length > 1) {
    		    
    		        $("select[name="+fieldNames[i]+"]").each(function(i){
    		            submitString += "&" + $(this).attr("id") + "=" + $(this).val();
    				});
                      
    			}
    			else {
    				  
    			    // input field and textarea values
    			    if ( $("#"+fieldNames[i]).val() ) {
    				
    					submitString += "&"+fieldNames[i]+"="+urlencode($("#"+fieldNames[i]).val());
    				}
    				else if ($("#"+fieldNames[i]+"_0").val()) {
    					submitString += "&"+fieldNames[i]+"="+urlencode($("#"+fieldNames[i]+"_0").val());
    				}
    			}
    		}            
            

            /**
             *  Send data with Ajax
             */
             
           
            $.ajax({
            	type	: "POST",
            	url		: "/si/ajax/",
            	data	: submitString,
            	async	: true,
            	success: function(messagetxt){
            		$("#q_space").html(messagetxt);
            		
            	}
            
            });
            
    	}
    }       
       
       
    function sendMailing(){

        
    	var validated 	= true;
    	var lang_id		= $("#lang_id").val();

		fieldNames 	= new Array("mailing_name", "mailing_email");
		fieldTypes 	= new Array("string", "email");
		required   	= new Array(true, true);
		
		colorTrue   = "black";
		colorFalse  = "red";

		label		= new Array("mailing_name_label","mailing_email_label");
		
		validated &= validateUserForm(fieldNames, fieldTypes, required, label, colorTrue, colorFalse);
      
    	/**
    	 *  Submit
    	 */
    	if (validated) {
    
            var submitString = "mailing=1";
            
    		for (var i in fieldNames) {
    		    // check if there are many fields with this field name
    		    if ($("input[name="+fieldNames[i]+"]").length > 1) {
    		    
    		        $("input[name="+fieldNames[i]+"]").each(function(i){
    		            submitString += "&" + $(this).attr("id") + "=" + urlencode($(this).val());
    				});
    
    			}
    			/**
    			 * Select boxes for person titles
    			 */                   			
                else if ($("select[name="+fieldNames[i]+"]").length > 1) {
    		    
    		        $("select[name="+fieldNames[i]+"]").each(function(i){
    		            submitString += "&" + $(this).attr("id") + "=" + $(this).val();
    				});
                      
    			}
    			else {
    				  
    			    // input field and textarea values
    			    if ( $("#"+fieldNames[i]).val() ) {
    				
    					submitString += "&"+fieldNames[i]+"="+urlencode($("#"+fieldNames[i]).val());
    				}
    				else if ($("#"+fieldNames[i]+"_0").val()) {
    					submitString += "&"+fieldNames[i]+"="+urlencode($("#"+fieldNames[i]+"_0").val());
    				}
    			}
    		}            
            

            /**
             *  Send data with Ajax
             */
             
           
            $.ajax({
            	type	: "POST",
            	url		: "/si/ajax/",
            	data	: submitString,
            	async	: true,
            	success: function(messagetxt){
            		$("#mailing_space").html(messagetxt);
            		
            	}
            
            });
            
    	}
    }       
/**
 *  @description:   This function valiadtes data enter by user.
 *
 *  @param array fieldNames 	: 	Array containing names of the fields to validate.
 *  @param array fieldTypes 	:   Array containing types of data that supose to be entered into fields.
 *  @param array required   	:   Array of boolean values telling which field is required and which is optional.
 *  @param array combineWith    :   Array containing label id names, to combine validation coloring with. For example:
 *                                  if value of field at first position is not valid and field value at some other position is valid
 *                                  and both fields share the same preceeding text label, second field overwrites first field.
 *                                  Usualy used for post_number following by post_name;
 *  @param array/strinmg colorTrue  : Array or string representing color of field preceeding label when field data is valid.
 *  @param array/strinmg colorFalse : Array or string representing color of field preceeding label when field data is not valid.
 *
 *  @returns                : Function return true if entered data is proper, otherwise it return false.
 *
 *  @author         		:   Jernej Gololicic
 */
function validateUserForm(fieldNames, fieldTypes, required, label, colorTrue, colorFalse){

	/**
	 *  Return this value
	 */
	var returnValue = true;

	for (var i in fieldNames) {
		if (required[i]) {
			switch ( fieldTypes[i] ) {
				case "string"   :   {
										var stringValue = $("#"+fieldNames[i]).val();
										if (stringValue && trim(stringValue) != "" ) {
                                         	colorPreceedingLabel(label[i], "black");
											returnValue &= true;
										}
										else {
	                                        colorPreceedingLabel(label[i], "red");
											returnValue &= false;
										}
										break;
									}
				case "integer"  :   {
										var intValue = $("#"+fieldNames[i] ).val();
										if ( isInteger(intValue) ) {
										
                                            colorPreceedingLabel(label[i], "black");
											returnValue &= true;
										}
										else {
	                                        colorPreceedingLabel(label[i], "red");
											returnValue &= false;
										}
										break;
									}
				case "email"	:   {
										var emailValue = $("#"+fieldNames[i] ).val();
										if ( isEmail(emailValue) ) {
										
                                            colorPreceedingLabel(label[i], "black");
											returnValue &= true;
										}
										else {
	                                        colorPreceedingLabel(label[i], "red");
											returnValue &= false;
										}
										break;
									}
			}

		}
	}
	return returnValue;
}
var MAX_DUMP_DEPTH = 10;
function dumpObj(obj, name, indent, depth) {
      if (depth > MAX_DUMP_DEPTH) {
             return indent + name + ": <Maximum Depth Reached>\n";
      }
      if (typeof obj == "object") {
             var child = null;
             var output = indent + name + "\n";
             indent += "\t";
             for (var item in obj)
             {
                   try {
                          child = obj[item];
                   } catch (e) {
                          child = "<Unable to Evaluate>";
                   }
                   if (typeof child == "object") {
                          output += dumpObj(child, item, indent, depth + 1);
                   } else {
                          output += indent + item + ": " + child + "\n";
                   }
             }
             return output;
      } else {
             return obj;
      }
}
