/**
 * @projectname     Zuza Hand Made
 * @version         1.0 2010.10.14
 * @copyright       Copyright (C) 2008 - 2010 All rights reserved.
 * @license         Commercial
 * @author          Székely Csaba / csaba@szekely.ro / http://www.csaba.szekely.ro
 * 
 * @desc            Shop display page
 */

// global variable for scrool
var sc_direction = 'left';
var sc_operation     = '-';
// global variable for auto scrool
var auto_scrool = true;

var cnt = 0;

 
// on page ready function 
$(document).ready(function(){     
    // animating move
    var auto_scrool = setInterval('animatePromotion(\'loop\')', 3500); 

    // if the mouse is over the line
    jQuery(".promotii_line_container").hover (
    // stopping auto scroll
        function () {
            // stop line auto scroll
            clearInterval(auto_scrool);
        },
        // whe mouse lives the line
        function () {
            // animating move
            auto_scrool = setInterval('animatePromotion(\'loop\')', 3500); 
        } // end fucn mouse live
    ); // mouse over end
  
    // right click 
    jQuery('.promotii_right_btn_container').click(function() {       
        cnt +=1;
        //moving products
        animatePromotion('left', 500);
    }); // end right click
     
    // right click 
    jQuery('.promotii_left_btn_container').click(function() { 
        cnt +=1;
        //moving products
        animatePromotion('right', 500);
    }); // end right click
    
    // cheking if form loaded
    if(jQuery("#product_details").length > 0){
        // validating anunturi form        
        jQuery("#product_details").validate({
            rules: {
                uniq_order_element: {
                    required: true
                }
            },
            messages: {
                uniq_order_element:{
                        required: "Selectati produsul"
                    }                
            }
        });
    } // end if length 

    // if on product page    
    if(jQuery("#product_details").length > 0){                           
        colorSelection(jQuery('.ddl_container').find('select'));
    } // end if on bying form

}); // end on ready

// moving product line 
function animatePromotion( direction, slide_speed ){      
    // the width of the image
    var imagebox_width = 94;
    // the position and dimension of the product line
    var product_line_width  = jQuery('.promotii_product_line_container').width();
    var window_width        = jQuery('.promotii_product_container').width();
    var widnow_position     = jQuery('.promotii_product_container').position();   
    var line_position       = jQuery('.promotii_product_line_container').position();    
    var sliding_speed       = slide_speed > 0 ? slide_speed : 3000;
    
    // if element shown
    if( product_line_width > window_width && jQuery('.promotii_product_line_container').length > 0 && !jQuery(".promotii_product_line_container").is(":animated")){
        // if called for button click left
        if(direction == 'left'){
            // if not at end of the list        
            if((line_position.left-1) > (widnow_position.left-(product_line_width-window_width)) && !jQuery(".promotii_product_line_container").is(":animated") ){
                // mooving line
                jQuery('.promotii_product_line_container').animate({"left": "-="+ imagebox_width +"px"}, sliding_speed);            
            }// end if line end
            
        // if called for button click left
        } else if (direction == 'right'){
            // if not at the start position
            if(line_position.left < widnow_position.left && !jQuery(".promotii_product_line_container").is(":animated")){
                // mooving line
                jQuery('.promotii_product_line_container').animate({"left": "+="+ imagebox_width +"px"}, sliding_speed);        
            }// end if at the start position
        } else if  (direction == 'loop'){
            // if at the end of the line 
            if(sc_direction == 'left' && (line_position.left-1) <= (widnow_position.left-(product_line_width-window_width))){
                // change direction 
                sc_direction = 'right';
                sc_operation = '+';
            // at the end of line
            } else if (sc_direction == 'right' && line_position.left >= widnow_position.left){
                // change direction
                sc_direction = 'left';
                sc_operation = '-';
            } // end if else end of line
            // moving line
            jQuery('.promotii_product_line_container').animate({"left": sc_operation+"="+ imagebox_width +"px"}, sliding_speed);        
            
        }// end if else right left
    } // end if exists element
} // end function promotion animation


 
// changing form action for submiting pagination
function changeFormAction (formaction){
    // setting form action 
    document.pagination_form.action = formaction;
    // submiting form
    document.pagination_form.submit();
} // end function


// cheking for selected properties
function chekProperties(){
    // document root form hidden input
    document_root = jQuery('#document_root').val();
    // setting select list color
    default_color = '#FFFFFF';
    error_color   = '#E81F7F';

    // propertyes selected (if ok nr of selects = all_properties_ok)
    all_properties_ok = true;
    // getting ddl selected values
    ddl_values          = ' ';
    ddl_filter_values   = ' ';

    // looping thru the selects
    jQuery(" #product_detail_selectors select").each(function(){
        // by default select background is white
        jQuery(this).css("background-color", default_color);
        // if there are not selected lists
        if(this.value == 0){
            // setting variable to false
            all_properties_ok = false;
            // coloring select background for visual
            jQuery(this).css({background: error_color});
        // if selected
        } else {
            // concatenating values
            ddl_values = ddl_values + '@@' + this.value; 
            // filter data for add to cart function
            ddl_filter_values = ddl_filter_values + '@@' + this.name + '||' + jQuery('#' + this.name + ' option:selected').text() + '||' + this.value;    
        }// end if else ddl not selected
    }); //end each select
    
    // if there are non selected ddl's 
    if( all_properties_ok == false ) {
        // displaying alert error
        alert(jQuery('#need_to_select').val()); 
    // if all are selected
    } else {
        // assigning filter data 
        jQuery('#h_filter_data').val(ddl_filter_values);
//        alert('ok');
        jQuery('#product_details').submit();
    } // end if else all pproerties ok           
} // end prod price func.



function colorSelection( triggerObject){
    // border color on selected image
    var border_color = ' #984F4F';
    // the containing div
    var main_parent  = triggerObject.parents().eq(1);
    
    if(main_parent.find('select').val() == 0){
        // setting all borders to default
        jQuery('.image_select_item').css('border', 'none');
    } // end if main call
    // if image click
    if(triggerObject.is('img')){ 
        // resetting borders
        jQuery('.image_select_item').css('border', 'none');
        // setting select value to triggered img ref.
        main_parent.find('select').val(triggerObject.attr('id')).attr('selected', 'selected');                
        // changing image border
        triggerObject.parent().css('border', '3px solid ' + border_color);
    // if select click
    } else if (triggerObject.is('select')){      
        // resetting borders
        jQuery('.image_select_item').css('border', 'none');
        // changing image border
        jQuery('.image_select_item#' + triggerObject.val()).css('border', '3px solid ' + border_color);
    } // end if else select / image click
} // end function 
