
jQuery.fn.startAjaxLoader = function ( loader_image_filename ) {
	if (this.data('ajax-loader-pre-html-content') == null) {
		this.data('ajax-loader-pre-html-content', this.html()).html('<img src="/images/'+(loader_image_filename != null ? loader_image_filename : 'ajax-loader-05.gif')+'" alt="..." />');
	}
	return this;
}

jQuery.fn.stopAjaxLoader = function () {
	if (this.data('ajax-loader-pre-html-content') != null) {
		this.html(this.data('ajax-loader-pre-html-content')).removeData('ajax-loader-pre-html-content');
	}
	return this;
}

function js_reload_current_page() {
  document.location.href = (document.location.href).replace(/#[^#]*$/i,'');
}

function js_prettyphoto_close() {
  jQuery.prettyPhoto.close();
  jQuery(document).unbind('keyup');
  return false;  
}

function js_prettyphoto_center() {
	var prettyphoto_element = jQuery('.pp_pic_holder:first');
	
	if ( prettyphoto_element.size() == 0 ) {
		return;	
	}
	
	var page_scroll = js_get_page_scroll();
	
	prettyphoto_element.css('left', (page_scroll[0] + Math.round( parseInt(jQuery(window).width()) / 2 ) - Math.round( parseInt(prettyphoto_element.width()) / 2 )).toString() + 'px');
	
	prettyphoto_element.css('top', (page_scroll[1] + Math.round( parseInt(jQuery(window).height()) / 2 ) - Math.round( parseInt(prettyphoto_element.height()) / 2 )).toString() + 'px');
}

function js_ssp_prettyphoto_close() {
  js_prettyphoto_close();  // default spravanie
  
  jQuery('.skvelyshop-button:first').poshytip('enable');
  jQuery('.p1_b, .p2_b, .p3_b, .p4_b, .p5_b').poshytip('enable');
  return false;
}

// username
function js_ssp_sk_lost_password_username_focus( input_element ) {
  if ( jQuery(input_element).hasClass('tooltip_input_def_text') ) {
    jQuery(input_element).val('').removeClass('tooltip_input_def_text');
  }
}

function js_ssp_sk_lost_password_username_blur( input_element ) {
  jQuery(input_element).val( jQuery.trim(jQuery(input_element).val()) );  
  if ( jQuery(input_element).val() == '' ) {
    jQuery(input_element).val(js_lang_ssp_sk_cms_lost_password_username_def_text).addClass('tooltip_input_def_text');
  }
}

// email
function js_ssp_sk_lost_password_email_focus( input_element ) {
  if ( jQuery(input_element).hasClass('tooltip_input_def_text') ) {
    jQuery(input_element).val('').removeClass('tooltip_input_def_text');
  }
}

function js_ssp_sk_lost_password_email_blur( input_element ) {
  jQuery(input_element).val( jQuery.trim(jQuery(input_element).val()) );  
  if ( jQuery(input_element).val() == '' ) {
    jQuery(input_element).val(js_lang_ssp_sk_cms_lost_password_email_def_text).addClass('tooltip_input_def_text');
  }
}

function js_ssp_sk_lost_password_form_send_click( link_element ) {
  link_element = jQuery(link_element);
  var container_element = link_element.parents('.lost_password_form_content:first');
  
  var email_input_element = container_element.find('input[name="confirmEmail"]:first');
  var email = jQuery.trim( email_input_element.val() );
  
  if ( email == '' || email_input_element.val() == js_lang_ssp_sk_cms_lost_password_email_def_text || /^[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}$/i.exec(email) == null ) {
    alert(js_lang_ssp_sk_cms_lost_password_email_fill);
    email_input_element.focus();
    return false;
  }
  
  var link_container_element = link_element.parent();
  link_container_element.startAjaxLoader();
  var spoof_hash_input_element = container_element.find('input[type="hidden"]:last');
  var ajax_params = {'confirmEmail':email,'checkusername':email};   
  
  ajax_params[spoof_hash_input_element.attr('name')] = spoof_hash_input_element.val();
      
  jQuery.post(skvelyweb_web_site+'/templates/'+cur_template+'/index.php?option=com_virtuemart&ajax_call=1&ajax_func=send_forgotten_password&no_html=1&no_vm_html=1&no_menu=1&no_css=1', ajax_params, function(res) {
             
    link_container_element.stopAjaxLoader();
        
    if ( res == '' ) {
      return false;  
    }
    
    if ( res == '0' ) {
      alert( js_lang_ssp_sk_cms_lost_password_error );
      email_input_element.focus();
      return false;
    }
    else {
    	var tooltip_content_element = link_container_element.parents('.lost_password_form_content:first');
    	
    	tooltip_content_element.find('table').remove();
    	tooltip_content_element.append( js_lang_ssp_sk_cms_lost_password_sent.replace('%s', email) );
    	
    	tooltip_content_element.append( '<div class="tooltip_save_cancel_buttons"><div class="save-icon-container"><a href="#" onclick="return js_prettyphoto_close();">'+js_lang_ssp_cms_button_close+'</a></div>' );
    	tooltip_content_element.find('.save-icon-container a').corner('4px');
    	
    	tooltip_content_element.parents('.pp_content:first').css('height', '98px');
    }
    
  });
  
  return false;
}

function js_ssp_sk_show_lost_password_form_click( link_element ) {
  link_element = jQuery(link_element);  
  var link_container_element = link_element;
  
  // formular uz bol vytvoreny
  if ( jQuery('body > .lost_password_show_form_link').size() != 0 ) {
    jQuery('body > .lost_password_show_form_link').click();
    return false;
  }
  
  link_container_element.startAjaxLoader();
  
  jQuery.post('/index2.php?option=com_registration&Itemid=f4d5020d35da4fee2ef45f877d9fdf1a&task=lostPassword&Itemid=f4d5020d35da4fee2ef45f877d9fdf1a&no_html=1', {}, function(res) {             
    link_container_element.stopAjaxLoader();
    
    //var save_title = /\<input type\=\"submit\"[^\>]*value\=\"([^\"]*)"/.exec(res)[1];
    var save_title = js_lang_ssp_cms_button_send;
        
    res = res.replace(/\<\/?form[^\>]*\>/g, '');
    res = res.replace(/\<input type\="submit"[^\>]+\>/, '<div class="save-icon-container"><a href="#" onclick="return js_ssp_sk_lost_password_form_send_click(this);">'+save_title+'</a></div><div class="cancel-icon-container"><a href="#" onclick="return js_ssp_prettyphoto_close();">'+js_lang_ssp_cms_button_close+'</a></div>');
    res = res.replace(/\<div class\="componentheading"\>\s*([^\<]*)\s*\<\/div\>/, '<h3>'+js_lang_ssp_sk_cms_lost_password_caption+'</h3>');
    res = res.replace(/\<table([^\>]*)cellspacing="[0-9]*"/, '<table$1cellspacing="0"');
    //res = res.replace(/\<input([^\>]*)name\="checkusername"/, '<input$1name\="checkusername" onfocus="js_lost_password_username_focus(this);" onblur="js_lost_password_username_blur(this);" value="'+js_lang_ssp_cms_lost_password_username_def_text+'"');
    res = res.replace(/\<input([^\>]*)name\="confirmEmail"/, '<input$1name\="confirmEmail" maxlength="200" onfocus="js_ssp_sk_lost_password_email_focus(this);" onblur="js_ssp_sk_lost_password_email_blur(this);" value="'+js_lang_ssp_sk_cms_lost_password_email_def_text+'"');
    
    //if ( typeof(console) != 'undefined' ) console.log( res );
    
    jQuery('body').append('<a href="#lost_password_form_container" class="lost_password_show_form_link"><!-- --></a>');
    
    var lost_pass_html = '';
    
    lost_pass_html += '<div id="lost_password_form_container">';
      lost_pass_html += '<div class="lost_password_form_content">';
      
        lost_pass_html += res;        
        lost_pass_html += '<div class="cs_prettyphoto_close"><a href="#" onclick="return js_ssp_prettyphoto_close();" title="'+js_lang_ssp_cms_button_close+'"><img src="/includes/js/jquery/prettyphoto/images/closelabel.png" alt="X"/></a></div>';
        
      lost_pass_html += '</div>';    
    lost_pass_html += '</div>';
    
    jQuery('body').append(lost_pass_html);
    
    var lost_password_container = jQuery('body > #lost_password_form_container');
    
    lost_password_container.find('td').each( function() {
      jQuery(this).html(jQuery.trim(jQuery(this).html()));     
    });
    lost_password_container.find('br').remove();
    lost_password_container.find('input[name="checkusername"]').parents('tr:first').remove();
    lost_password_container.find('input[name="confirmEmail"]').each( function() {
      jQuery(this).addClass('tooltip_input_def_text');
      jQuery(this).parents('tr:first').find('td:first').html(js_lang_ssp_sk_cms_lost_password_email).addClass('lost_password_label_td');
    });  
    
    lost_password_container.find('tr:first').find('td:first').html(js_lang_ssp_sk_cms_lost_password_info_text);
        
    jQuery('body > .lost_password_show_form_link').attr('href','#lost_password_form_container').prettyPhoto({
      show_title: false,
      allow_resize: false,
      modal: true,
      theme: 'facebook_yellow',
      keyboard_shortcuts: false,
      default_width: 396,
      
      changepicturecallback: function() {
        var prettyphoto_element = jQuery('body > .pp_pic_holder:last');
        prettyphoto_element.find('.pp_content:first').css({'height':'140px', 'background-color':'#FFFFE1', 'padding-left':'10px', 'padding-right':'10px', 'padding-top':'10px', 'padding-bottom':'10px'});
        prettyphoto_element.find('.save-icon-container a').corner('4px');
        prettyphoto_element.find('input:first').focus();
      }
    }).click();
  });  
  
  return false;
}

function js_ssp_sk_show_login_form_click( link_element ) {
  link_element = jQuery(link_element);  
  var link_container_element = link_element;
  
  // formular uz bol vytvoreny
  if ( jQuery('body > .login_show_form_link').size() != 0 ) {
    jQuery('body > .login_show_form_link').click();
    return false;
  }
  
  link_container_element.startAjaxLoader();

  ajax_params = {'option': 'com_virtuemart', 'ajax_call': '1', 'ajax_func': 'get_login_form_html', 'no_html': '1', 'no_vm_html': '1', 'no_menu': '1', 'no_css': '1', 'hide_remember_section': '1', 'hide_lost_password_section': '1'};
  jQuery.post(skvelyweb_web_site+'/templates/'+cur_template+'/index.php', ajax_params, function(res) {
    link_container_element.stopAjaxLoader();

    //var save_title = /\<input type\=\"submit\"[^\>]*value\=\"([^\"]*)"/.exec(res)[1];
    var save_title = js_lang_ssp_cms_button_send;

    res = res.replace('name="client_section"', 'name="templ_client_section"');;
if (0) {
    res = res.replace(/\<\/?form[^\>]*\>/g, '');
    res = res.replace(/\<input type\="submit"[^\>]+\>/, '<div class="save-icon-container"><a href="#" onclick="return js_ssp_sk_lost_password_form_send_click(this);">'+save_title+'</a></div><div class="cancel-icon-container"><a href="#" onclick="return js_ssp_prettyphoto_close();">'+js_lang_ssp_cms_button_close+'</a></div>');
    res = res.replace(/\<div class\="componentheading"\>\s*([^\<]*)\s*\<\/div\>/, '<h3>'+js_lang_ssp_sk_cms_lost_password_caption+'</h3>');
    res = res.replace(/\<table([^\>]*)cellspacing="[0-9]*"/, '<table$1cellspacing="0"');
    //res = res.replace(/\<input([^\>]*)name\="checkusername"/, '<input$1name\="checkusername" onfocus="js_lost_password_username_focus(this);" onblur="js_lost_password_username_blur(this);" value="'+js_lang_ssp_cms_lost_password_username_def_text+'"');
    res = res.replace(/\<input([^\>]*)name\="confirmEmail"/, '<input$1name\="confirmEmail" maxlength="200" onfocus="js_ssp_sk_lost_password_email_focus(this);" onblur="js_ssp_sk_lost_password_email_blur(this);" value="'+js_lang_ssp_sk_cms_lost_password_email_def_text+'"');
}

    //if ( typeof(console) != 'undefined' ) console.log( res );

    jQuery('body').append('<a href="#login_form_container" class="login_show_form_link"><!-- --></a>');

    var lost_pass_html = '';

    lost_pass_html += '<div id="login_form_container" style="display:none">';
    lost_pass_html += '<div class="login_form_content">';
    lost_pass_html += '<span class="module-name login_form">Prihlásenie do administrácie e-shopu</span>';
    lost_pass_html += res;        
    lost_pass_html += '<div class="cs_prettyphoto_close"><a href="#" onclick="return js_ssp_prettyphoto_close();" title="'+js_lang_ssp_cms_button_close+'"><img src="/includes/js/jquery/prettyphoto/images/closelabel.png" alt="X"/></a></div>';
        
    lost_pass_html += '</div>';    
    lost_pass_html += '</div>';
    
    jQuery('body').append(lost_pass_html);
if (0) {
    var login_container = jQuery('body > #login_form_container');
    
    login_container.find('td').each( function() {
      jQuery(this).html(jQuery.trim(jQuery(this).html()));     
    });
    login_container.find('br').remove();
    login_container.find('input[name="checkusername"]').parents('tr:first').remove();
    login_container.find('input[name="confirmEmail"]').each( function() {
      jQuery(this).addClass('tooltip_input_def_text');
      jQuery(this).parents('tr:first').find('td:first').html(js_lang_ssp_sk_cms_lost_password_email).addClass('login_label_td');
    });  
    
    login_container.find('tr:first').find('td:first').html(js_lang_ssp_sk_cms_lost_password_info_text);
}
    jQuery('body > .login_show_form_link').attr('href','#login_form_container').prettyPhoto({
      show_title: false,
      allow_resize: false,
      modal: true,
      theme: 'facebook_yellow',
      keyboard_shortcuts: false,
      default_width: 396,
      
      changepicturecallback: function() {
        var prettyphoto_element = jQuery('body > .pp_pic_holder:last');
        prettyphoto_element.find('.pp_content:first').css({'height':'75px', 'background-color':'#FFFFE1', 'padding-left':'10px', 'padding-right':'10px', 'padding-top':'10px', 'padding-bottom':'10px'});
        prettyphoto_element.find('.save-icon-container a').corner('4px');
        prettyphoto_element.find('form[name="templ_client_section"]').attr('name', 'client_section');
        prettyphoto_element.find('form[name="client_section"]').append('<input type="hidden" name="client_section_no_process_failed_login" value="1" />');
        js_show_mod_login_ready();
        prettyphoto_element.find('input:first').focus();
      }
    }).click();
  });  
  
  return false;
}

// on ready
jQuery(window).load(function() {
   
	jQuery('a.href_lost').attr('href','#').bind('click', function() {
		return js_ssp_sk_show_lost_password_form_click( jQuery(this) );          
    });

	jQuery('a.p_e').attr('href','#').bind('click', function() {
		return js_ssp_sk_show_login_form_click( jQuery(this) );          
    });

  jQuery("a[rel^='prettyPhoto']").each(function() {

   if ( jQuery(this).hasClass('movie-home') || jQuery(this).hasClass('movie-video') ) {
   
     jQuery(this).prettyPhoto({
      show_title: false,
      changepicturecallback: function() {
        var prettyphoto_element = jQuery('body > .pp_pic_holder:last');
        prettyphoto_element.find('.pp_content:first').css('height', (parseInt(prettyphoto_element.find('.pp_content:first').height())-28).toString()+'px' );
        prettyphoto_element.find('.pp_content:first').append('<div class="cs_prettyphoto_close"><a href="#" onclick="return js_ssp_prettyphoto_close();" title="Zatvoriť"><img src="http://www.skvelyshop.sk/includes/js/jquery/prettyphoto/images/closelabel.png" alt="X"/></a></div>');
      },
      theme: 'dark_rounded'
     });     
   }
   else {

     jQuery(this).prettyPhoto({
      show_title: false,  
      theme: 'dark_rounded'
     });
   }

 });
});

function js_htmlcaption4_clicked() {
	jQuery('#video-link a.movie-home').click();
	return false;
}

// FAQ highlighting -----------------------------------------------------------------------------------------------------------
function js_faq_item_highlight(url_hash) {
	if (window.location.pathname != '/faq/')
		return;
	
	jQuery('.faq-table td').removeClass('faq_active');	// remove active
	jQuery('.faq-table td .faq_cont').removeClass('faq_active');
	
	if (jQuery('.faq-table a[name='+url_hash+']').parent('.faq_cont').size() > 0)
		jQuery('.faq-table a[name='+url_hash+']').parents('.faq_cont').addClass('faq_active');	// add active		
	else	
		jQuery('.faq-table a[name='+url_hash+']').parents('td:first').addClass('faq_active');	// add active
}

jQuery(function () {	
	var url_hash = window.location.hash.replace(/#/gi, '');
	js_faq_item_highlight(url_hash);
	
	jQuery('ul.faq-list a').bind('click', function (e) {
		if( e.preventDefault )
			e.preventDefault();	
		
		var this_link = jQuery(this).attr('href').split('#');
		if (this_link.length < 2)
			return;
		
		
		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var trgt = this_link[1];

		//get the top offset of the target anchor
		var target_offset = jQuery("[name="+trgt+"]").offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		jQuery('html, body').animate({scrollTop:target_top - 15}, 700);
		
		js_faq_item_highlight(this_link[1]);
	});
	
	jQuery('.faq-table td').each(function () {
		var upper = '<a href="#" class="faq_upper" onclick="jQuery(\'html, body\').animate({scrollTop:0}, 700); return false;">&uArr;</a>';
			
		if (jQuery(this).find('.faq_cont').size() > 0) 
			jQuery(this).find('.faq_cont').append(upper);
		else		
			jQuery(this).append(upper);
	});
});
// \\FAQ highlighting -----------------------------------------------------------------------------------------------------------
