$(document).ready(function()
{
/*GLOBALS STYLE ADJUSTMENT--------------------------------------------------------*/	
	$.style = function(selector, options){
		options = $.extend ({type: 'text/css', media: 'all'}, options);
		var style = $(document.createElement('style')).attr(options).appendTo('head')[0];
		if (style.styleSheet){
			// IE
			var sheet = style.styleSheet;
			sheet.addRule(selector, null);
			return $(sheet.rules[0]);
		}else if (style.sheet){
			// Firefox
			sheet = style.sheet;
			sheet.insertRule(selector+' {}',0);
		return $(sheet.cssRules[0]);
		}
	}; 

/*HELP TIPS --------------------------------------------------------*/	
$.fn.qtip.styles.helptip = { 
   background: '#c4dbf7',
   color: 'black',
   border: {
      width: 1,
      radius: 5,
      color: '#c4dbf7'
   },
   tip: 'leftMiddle',
   name: 'dark' 
};

$('.helptip').livequery(function(){
	$(this).qtip({ style: { name: 'helptip', tip: true },position:{corner:{target:'rightMiddle',tooltip:'leftMiddle'}}});
});		


/*NOTE TIPS --------------------------------------------------------*/	
$.fn.qtip.styles.notetip = { 
   background: '#ffd800',
   color: 'black',
   border: {
      width: 1,
      radius: 5,
      color: '#ffd800'
   },
   tip: 'leftMiddle',
   name: 'dark' 
};

$('.notetip').livequery(function(){
	$(this).qtip({style: { name: 'notetip', tip: true },position:{corner:{target:'rightMiddle',tooltip:'leftMiddle'}}});
});		


//HOVER IMGS
	$('.hvr_img').live('mouseover', function(e){ 
		var src = $(this).attr('src');
		src = src.replace('.jpg','_o.jpg');		
		src = src.replace('.png','_o.png');
		src = src.replace('.gif','_o.gif');
		$(this).attr('src',src);
	});

	$('.hvr_img').live('mouseout', function(e){ 
		var src = $(this).attr('src');
		src = src.replace('_o.jpg','.jpg');		
		src = src.replace('_o.png','.png');
		src = src.replace('_o.gif','.gif');
		$(this).attr('src',src);
	});

//CONVERT FORMS TO AJAX FORMS
	$('.v6form').livequery(function(){
		$('#' + $(this).attr('id')).ajaxForm({target: '#vfrm_' + $(this).attr('id')});
		
		//hide control divs
		$('#pfrm_' + $(this).attr('id')).hide();
		$('#enotice_' + $(this).attr('id')).hide();
		$('#submit_' + $(this).attr('id')).hide();
	});

//CONVERT DATAFILTERS TO AJAX FORMS
/*
	$('.v6dataFilter').livequery(function(){
		$('#' + $(this).attr('id')).ajaxForm({target: '#' + $(this).attr('id') + '_result'});
		
		//hide submit button
		$('#submit_' + $(this).attr('id')).hide();
	});
*/

//DATAGRID ROW HIGHLIGHTING
	$('.datagrid tr').live('mouseover', function(){ $(this).addClass("datagrid_over"); });
	$('.datagrid tr').live('mouseout', function(){ $(this).removeClass("datagrid_over"); });


//CONVERT DATAGRID POSTS TO AJAX FORMS
	$('.v6dataGrid').livequery(function(){
		$('#' + $(this).attr('id')).ajaxForm({target: '#' + $(this).attr('id') + '_container'});
	});

//ADD HOVER TO DATAGRID ORDER BY COLS
	$('.datagrid_col_ob').live('mouseover', function(){ $(this).addClass("datagrid_col_ob_desc"); });
	$('.datagrid_col_ob').live('mouseout', function(){ $(this).removeClass("datagrid_col_ob_desc"); });


//ADD HOVER TO DATAGRID FOOTER PAGES
	$('.datagrid_pg').live('mouseover', function(){ $(this).addClass("datagrid_over"); });
	$('.datagrid_pg').live('mouseout', function(){ $(this).removeClass("datagrid_over"); });



//ADD HOVER TO PRODLIST PAGES
	$('.listprods_pg').live('mouseover', function(){ $(this).addClass("listprods_pg_over"); });
	$('.listprods_pg').live('mouseout', function(){ $(this).removeClass("listprods_pg_over"); });


//CONTROL LINKS------------------------------------------------------------

  $('a').live('click', function(e) {
    var ahref = $(this).attr('href');
	if (ahref) {
      if( (ahref.substr(0,7) == 'http://') && (ahref.substr(7,hostname.length) != window.location.hostname) ) {
        e.preventDefault();
        window.open($(this).attr('href'));
        return false;
      }
      else if (($(this).attr('rel') == 'dialog')||($(this).attr('rel') == 'Dialog')) {
        e.preventDefault();
        dialog(this.href);
        return false;
      }
      else if (($(this).attr('rel') == 'modal')||($(this).attr('rel') == 'Modal')) {
        e.preventDefault();
        dialog(this.href,1);
        return false;
      }
	}
  });

//TABS------------------------------------------------------------

    var $doctabs = $('#contentTabs').tabs({
		cache: 'true',

        add: function(e, ui) {
            // append close icon
            $(ui.tab).parents('li:first')
                .append('<em class="ui-tabs-close" title="Close Tab"><img onmouseover="this.src = \'/admin/images/tabs/close_hover.png\';" onmouseout="this.src = \'/admin/images/tabs/close.png\';" src="/admin/images/tabs/close.png"></em>')
                .find('.ui-tabs-close')
                .click(function() {
                    $doctabs.tabs('remove', $('li', $doctabs).index($(this).parents('li:first')[0]));
                });
            
            // select just added tab
            $doctabs.tabs('select', '#' + ui.panel.id);
        },                  

	    load: function(e, ui) {
			$doctabs.tabs('select', '#' + ui.panel.id);
			$(".ui-tabs-selected > a > span").html($('#' + ui.panel.id + ' > .title').html());
	    }

	});

	$('li', $doctabs)
		.append('<em class="ui-tabs-close" title="Close Tab"><img onmouseover="this.src = \'/admin/images/tabs/close_hover.png\';" onmouseout="this.src = \'/admin/images/tabs/close.png\';" src="/admin/images/tabs/close.png"></em>')
		.find('em')
		.click(function() 
		{
			$doctabs.tabs('remove', $('li', $doctabs).index($(this).parents('li:first')[0]));
		});


//LOAD ANY AJAX PAGES SPECIFIED INLINE
	$('.ajaxLoad').livequery(function(){
        var myDate = new Date();
		var myurl = $(this).attr('rel');
		var hasq = myurl.indexOf("?");
		if (hasq == -1) {
          var url = myurl+"?ts="+myDate.getTime();	
		}
		else {
		  var url = myurl+"&ts="+myDate.getTime();	
		}
		ajaxLoad('#' + $(this).attr('id'),url);
	});

});


function checkbox()
{

	if ($("#mailcheck").attr("checked") != true) 
	{
		alert("Please agree to the terms and conditions.");
		return false;  
	}	
	else
	{
		return true;
	} 
}

function checkbox2()
{ 
	if ($("#mailcheck2").attr("checked") != true) 
	{
		jAlert("Please agree to the trems and conditions.");
		return false;  
	}	
	else
	{
		return true;
	} 
}


function ajaxLoad(id,url)
//loads ajax page to a specific div 
{
	$(id).html('<img src="/images/ajax-loader.gif">');
    $(id).load(url);
}


function addtab(tab_id,href)
//adds a tab
{
	$(tab_id).tabs( "add", href);
}

function loadtab(tab_id,url)
//loads a tab
{
 	if (url.indexOf('?') == -1) url = url + '?g=' + randomString();
	else url = url + '&g=' + randomString();		

	var cur = $(tab_id).tabs().data("selected.tabs");

	$(tab_id).tabs("url", cur, url);
	$(tab_id).tabs("load", cur);
}

function reloadtab(tab_id)
//reloads tab
{
	var cur = $("#contentTabs").tabs().data("selected.tabs");
	$(tab_id).tabs("load", cur);
}

function srch_clr()
{
	if (document.srch_frm.srch.value == 'Enter Text')
		document.srch_frm.srch.value = '';
}

function srch_reset()
{
	if (document.srch_frm.srch.value == '')
		document.srch_frm.srch.value = 'Enter Text';
}

function addtocart_prod(prodID,divID)
{
	ajaxLoad('#' + divID,'/applets/addtocart.php?prodID=' + prodID);
}

function addtocart_prod_extra(prodID,divID,extraClass,formats)
{
	ajaxLoad('#' + divID,'/applets/addtocart.php?extraclass=' + extraClass + '&prodID=' + prodID + '&formats='+ formats);
}

function addtocartGiftReg_prod(grID,prodID,divID)
{
	var qtySel	= document.getElementById('GRqty' + prodID);
	var sIDx	= qtySel.selectedIndex;
	var qty		= qtySel[sIDx].value;	
	
	ajaxLoad('#' + divID,'/applets/addtocart.giftreg.php?prodID=' + prodID + '&grID=' + grID + '&qty=' + qty);
}

function refreshCart()
{
	if ($('#cart').length) ajaxLoad('#cart','/incls/cms/cart/index.php');
}

function refreshWishlist()
{
	if ($('#miniwishlist').length) ajaxLoad('#miniwishlist','/incls/cms/minilist.php');
}

function redirect(url)
{
	window.location.href = url;
}


function addtoregistry_prod(grID, prodID, divID) {
  ajaxLoad('#' + divID,'/applets/addtoregistry.php?grID=' + grID + '&prodID=' + prodID);
}

function addtoregistry_prod_extra(grID, prodID, divID, extraClass) {
  ajaxLoad('#' + divID,'/applets/addtoregistry.php?extraclass=' + extraClass + '&grID=' + grID + '&prodID=' + prodID);
}

function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

function hide(div) {
  el = document.getElementById(div);
  el.style.display = "none";
}

function show(div) {
  el = document.getElementById(div);
  el.style.display = "block";
}

function toggle(obj) {
  if ($('#'+obj).is(':visible')) {
    $('#'+obj).hide();
  }
  else {
    $('#'+obj).show();
  }
}

function transfer(div1, div2, val) {
  if (val != "") {
    document.getElementById(div2).value = str_replace(val, "%20", " ");
  }
  else {
    document.getElementById(div2).value = str_replace(document.getElementById(div1).innerHTML, "%20", " ");
  }
}

/*
function transfer(div1, div2, val) {
  if (val != "") {
    document.getElementById(div2).value = val;
  }
  else {
    document.getElementById(div2).value = document.getElementById(div1).innerHTML;
  }
}
*/

function launchPayment(oID) {
  jConfirm('Click OK to make an EFT payment. Please remember to use your order number ('+oID+') as reference.', 'Info', function(r) {
    if (r) {
      window.location.href = "/account/orders/eftpayment.php?oID="+oID;
    }
  });  
}


