/**
 * Functions for all modules
 */

// overwrite jquery default object ($) with "jq" (jQuery is still available)
jq = jQuery.noConflict();

// Superfish menu
jQuery(function(){
//	jQuery('ul.sf-menu').superfish();
});

// jQuery product slider
jq(document).ready(function() {

	//rotation speed and timer
	var speed = 5000;
	var run = setInterval('rotate()', speed);	
	
	//grab the width and calculate left value
	var item_width = jq('#slides li').outerWidth(); 
	var left_value = item_width * (-1); 
        
    //move the last item before first item, just in case user click prev button
	jq('#slides li:first').before(jq('#slides li:last'));
	
	//set the default item to the correct position 
	jq('#slides ul').css({'left' : left_value});

    //if user clicked on prev button
	jq('#prev').click(function() {

		//get the right position            
		var left_indent = parseInt(jq('#slides ul').css('left')) + item_width;

		//slide the item            
		jq('#slides ul:not(:animated)').animate({'left' : left_indent}, 300,function(){    

            //move the last item and put it as first item            	
			jq('#slides li:first').before(jq('#slides li:last'));           

			//set the default item to correct position
			jq('#slides ul').css({'left' : left_value});
		
		});

		//cancel the link behavior            
		return false;
            
	});

 
    //if user clicked on next button
	jq('#next').click(function() {
		
		//get the right position
		var left_indent = parseInt(jq('#slides ul').css('left')) - item_width;
		
		//slide the item
		jq('#slides ul:not(:animated)').animate({'left' : left_indent}, 300, function () {
            
            //move the first item and put it as last item
			jq('#slides li:last').after(jq('#slides li:first'));                 	
			
			//set the default item to correct position
			jq('#slides ul').css({'left' : left_value});
		
		});
		         
		//cancel the link behavior
		return false;
		
	});        
	
	//if mouse hover, pause the auto rotation, otherwise rotate it
	jq('#slides').hover(
		
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('rotate()', speed);	
		}
	);

    // toggle categories menu
    jq('ul.sf-menu').superfish();
});
//a simple function to click next link
//a timer will call this function, and the rotation will begin :)  
function rotate() {
	jq('div#buttons div#next').click();
}

function get_suppliers(category_id)
{
	var url = base_url + 'home/suppliersByCategory/'+category_id;
	jq.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        success: function( resp ){
        	jq('#categories_names').
            html(resp.categories);
        	jq('#tab1').
            html(resp.suppliers);
        	jq('#fournisseurs_number').
        	html(resp.suppliers_number);
        }});
}

function add_new_contact()
{
	var id = 0;
	var number = jq('#new_contacts_number').val();
	var name1 = jq('input#newContactName_1');
	var name2 = jq('input#newContactName_2');
	var name3 = jq('input#newContactName_3');
	var name4 = jq('input#newContactName_4');
	
	if (name1.length == 0)
	{
		id = 1;
	}
	else if (name2.length == 0)
	{
		id = 2;
	}
	else if (name3.length == 0)
	{
		id = 3;
	}
	else if (name4.length == 0)
	{
		id = 4;
	}
	
	
	number = parseInt(number) + 1;
	
	if (number == 4)
	{
		jq('#other_contact').hide();
	}
	
	jq('#new_contacts_number').val(number);
	var url = base_url + 'wizard/new_contact/'+id;
	jq.ajax({
        url: url,
        type: 'get',
        success: function( resp ){
        	jq('#contacts_table').append(resp);
            bindRadios();
        }});
}

function wizard_page2(resp)
{
     jq('div#smartwizard').html(resp.content);
     validation_wizard_index_contacts();
     bindRadios();
}

function wizard_page3(resp)
{
     jq('div#smartwizard').html(resp.content);
     validation_wizard_index();
     bindRadios();
}

function wizard_page4(resp)
{
     jq('div#smartwizard').html(resp.content);
     bindRadios();
}

function delete_contact(id)
{
	var name = '#' + 'newContact_' + id;
	var number = jq('#new_contacts_number').val();
	number = parseInt(number) - 1;
	jq('#new_contacts_number').val(number);
	jq(name).remove();
	if (number == 3)
	{
		jq('#other_contact').show();
	}
}

function show_new_function()
{
	if (jq('#functions_dropdown').val() == 100000)
	{
		jq('#new_function_input').show();
	}
	else
	{
		jq('input#new_function_input').val('');
		jq('#new_function_input').hide();
	}
}

function frontend_login(resp)
{
     jq('div#login_area').html(resp.content);
}

function ajaxLogout( ajaxUrl ){
    // execute ajax request
	jq.ajax({
        url: ajaxUrl,
        type: 'post',
        dataType: 'json',
        success: function(objResp){
        jq('div#login_area').html(objResp.content);
        }
    });
}

function loadSmartWizard(){
    jq('.wiz-container').smartWizard({
        animation:false
    });
}

function hide_show_mail_tr(distributor_id)
{
    jq('tr.mail_tr_'+distributor_id).toggle();
}

function send_distributor_contacts_mail(distributor_id)
{
    jq('tr.mail_tr_'+distributor_id).toggle();
}

function empty_mail_fields(resp)
{
    if (resp.distributor_id == -1)// when is sent mail for suppliers contacts distributor_id = -1
    {
        jq('#subject').val('');
        jq('#message').val('');
    }
    else
    {
        jq('#subject_'+resp.distributor_id).val('');
        jq('#message_'+resp.distributor_id).val('');
    }
    
}

