/* - - - global APP js - - - */
//to determine if an ajax page has been called directly
var js_APP_GLOBAL = true;
//////////////////////////////

$(document).ready(function() {
    
    //user top menu - if logged add count unread msg, user account link
    if ($.isFunction($.cookie)) {
	    if ( $.cookie('c_user_login') ) {
	        
	        $('#topmenu-user').html(        
	            'Hi <b><a href="/user/profile.php?u=' + $.cookie('c_user_login') + '">' + $.cookie('c_user_login') + '</a></b>' +
	            ' :: ' +
	            '<a href="/messages/list.php?mtype=inbox">' + _jt("Messages")  + ' (<b>' +  $.cookie('c_unread_messages') + '</b>)</a>' +
	            ' | ' +
	            '<a href="/user/logout_process.php">' + _jt("Sign Out") + '</a>'                        
	        );
	        
	        
	        if ( $.cookie('c_admin') ) {
	            $('#topmenu-user').html(
	                $('#topmenu-user').html() + 
	                ' | ' +
	                '<a href="/admin/index.php">Admin</a>'
	            );    
	        }
	                            
	    } 
    }
    

});

$(document).ready(function() {
	//put a nice round corner to various elements.. :) I LOVE JQUERY!!!
// 	$('input.btnGeneral').addClass("corner4");
 	$('div.search-results').addClass('corner4');
 	$('table.results_container').addClass('corner4');
 	$('div.block_container').addClass('corner4');
 	$('div.general-box').addClass('corner4');
 	$('#general-box').addClass('corner4');
 	$('div.block').addClass('corner4');
	   
    
	//do corners.... - we now use css3
	//$('.corner').corner('keep'); 
    //alert(_("test2"));
    //$('.corner4').corner('keep 4px');
	
	//corners for IE with curvyCorners lib...
	//drop support for IE < 9
	//if ( $.browser.msie ) {	
	//	var IE_cc_settings = {
	//      tl: { radius: 4 },
	//      tr: { radius: 4 },
	//      bl: { radius: 4 },
	//      br: { radius: 4 },
	//      antiAlias: true
	//    }
	//    curvyCornersVerbose = false;
	    //all of these do not work correctly... heights f**ked...
	    //curvyCorners(IE_cc_settings, 'div.search-results');
	    //curvyCorners(IE_cc_settings, 'table.results_container');
	    //curvyCorners(IE_cc_settings, 'div.block_container'); 
	    //curvyCorners(IE_cc_settings, '#general-box');
		//curvyCorners(IE_cc_settings, 'div.general-box');
	//	curvyCorners(IE_cc_settings, 'div.block');
		//todo with pie... with relative position.... bug...	
	//} 
  
});

    
function faq_toggle_item(id_show)
{
    if ( $('#' + id_show) ) {
        if ( $('#' + id_show).css('display') == 'block' ) {
            $('#' + id_show).hide('fast');
            $('#' + id_show).css('display', 'none');
            return;        
        }
    }
    
    $('#' + id_show).show('fast');
    $('#' + id_show).css('display', 'block');
}



function openWinUsernameCheck() {

	var f = document.form_user_register;
    
    //trim
    f['user_login'].value = TrimAll(f['user_login'].value);
    
	var user_login = f['user_login'].value;

    //check if blank	
    if (isBlank(user_login)) {
        alert(_jt("Please type a username and re-check."));
        return;
    }
    
    //check user_login
	var test_pattern = /^[A-Za-z0-9]{5,32}$/;
	if (!test_pattern.test(user_login)) {
		alert(_jt("Invalid Username. \n Please use 5 to 25 characters and use only english letters and/or numbers. \n Please re-enter."));
		return;
	}
    
    
    //now open the popup win
    
    var winWidth  = 400;
    var winHeight = 200;

    var winPosWidth = (screen.availWidth - winWidth) / 2;
    var winPosHeight = (screen.availHeight - winHeight) / 2;    

    
    window.open('./check_username.php?user_login=' + user_login, 'winCheckUsername', 'left=' + winPosWidth + ', top=' + winPosHeight + ', width=' + winWidth + ', height=' + winHeight + ', status=no, toolbar=no, menubar=no, resizable=no, location=no, scrollbars=no');
}


function toggleCoinPostMethodOther() {
 
    var f = document.form_offer_get;
    
    if (document.getElementById('pm_other').checked) {
        //document.getElementById('offer_shipment_method_other_LAYER').style.display = 'block';
        $('#offer_shipment_method_other_LAYER').show('fast');
    } else {
        //document.getElementById('offer_shipment_method_other_LAYER').style.display = 'none';
        $('#offer_shipment_method_other_LAYER').hide('fast');
    }

}


function toggleCoinPayMethodOther() {
 
    var f = document.form_coin_insert;
    
    if (document.getElementById('pm_other').checked) {
        document.getElementById('coin_pay_method_other_LAYER').style.display = 'block';
    } else {
        document.getElementById('coin_pay_method_other_LAYER').style.display = 'none';
    }

}


function toggleCoinAreas(show_layer, label_bold) {
//oct 15: continents not used...
 
    var f = document.form_coin_insert;

    //hidden layers
    var all_layers = new Array('coin_countries_LAYER', 'coin_continents_LAYER'); 

    for ( var i = 0; i < all_layers.length; i++) {
    	if ( document.getElementById(all_layers[i]) ) {
        	document.getElementById(all_layers[i]).style.display = 'none';
        }
    }

    if (show_layer != '') {
        document.getElementById(show_layer).style.display = 'block';
    }
    
    ////////////////////
        
    var all_labels = new Array('label_countries_all', 'label_countries_selected', 'label_continents_selected');
    
    //first hide all layers
    for ( var i = 0; i < all_labels.length; i++) {
    	if ( document.getElementById(all_labels[i]) ) {
        	document.getElementById(all_labels[i]).style.fontWeight = 'normal';
        }
    }    
    
    document.getElementById(label_bold).style.fontWeight = 'bold';

}

function toggleCoinOfferCategories(show_layer, label_bold) {
    var f = document.form_coin_insert;

    //hide layer
    document.getElementById('categories_selection_LAYER').style.display = 'none';


    if (show_layer != '') {
        document.getElementById(show_layer).style.display = 'block';
    }    
    ////////////////////
        
    var all_labels = new Array('label_coin_offer_categories_all', 'label_coin_offer_categories_select');
    
    //first hide all layers
    for ( var i = 0; i < all_labels.length; i++) {
    	if ( document.getElementById(all_labels[i]) ) {
        	document.getElementById(all_labels[i]).style.fontWeight = 'normal';
        }
    }    
    
    document.getElementById(label_bold).style.fontWeight = 'bold';
}


function cmMail(domain, user) { 
   document.location.href = "mailto:" + user + "@" + domain; 
}

function makeSelectedCategoryButton() {

    if ( document.getElementById('btn'+btnNumberSelected) ) {
        document.getElementById('btn'+btnNumberSelected).src = 'theme/images/btn_'+langDefault+'/'+btnNumberSelected+'_over.gif';
    }
}

function categorySelectSetChoice( cid, catetgory_string )
{
	if ( typeof js_category_select_from_IC != 'undefined' ) {
		//var exVal = window.opener.$('#coin_offer_categories_select_hidden').val();
		//window.opener.$('#coin_offer_categories_select_hidden').val(exVal + ',' + cid);
		//alert(window.opener.$('#coin_offer_categories_select_hidden').val());
		//window.opener.$('#coin_offer_categories_select_html').html(catetgory_string);

		//check if category alredy selected
		if ( window.opener.document.getElementById('category_selected_' + cid) ) {
			alert( _jt("Category already selected!") );
			return;
		}				
				
		//set internal o... comma sep values
		if ( window.opener.$('#coin_offer_categories_select_hidden').val() == '' ) {
			window.opener.$('#coin_offer_categories_select_hidden').val(cid);
			var counter_selected_categories = 0;
		} else {
			var tem_current_val = window.opener.$('#coin_offer_categories_select_hidden').val();
			counter_selected_categories = tem_current_val.split(',').length;
			if ( counter_selected_categories >= 10 ) {
				alert( _jt("You have reached the limit of 10 categories!") );
				self.close();
				return;
			//	window.opener.$('#link_select_categories').hide();
			}			
					
			window.opener.$('#coin_offer_categories_select_hidden').val( window.opener.$('#coin_offer_categories_select_hidden').val() + ',' + cid );
			var tem_new_val = window.opener.$('#coin_offer_categories_select_hidden').val();
			counter_selected_categories = tem_new_val.split(',').length;
		}
        
		window.opener.$('#append_categories_selected_html').append('<div id="category_selected_' + cid + '"><span class="category_selected_span">' + catetgory_string + ' <a href="#" onclick="coinRemoveCategorySelected(' + cid + '); return false;">x</a></span></div>');

		if ( counter_selected_categories >= 10 ) {
			self.close();
			return;
		}
		
		return;					
	} 
	
	//default mode: single selection in create offer and edit offer by admin
	window.opener.$('#offer_category_id').val(cid);
	window.opener.$('#offer_category_id_NEW').html(catetgory_string);
	self.close();
	
	
	
	
}

function coinRemoveCategorySelected(cid) {
		if ( document.getElementById('category_selected_' + cid) ) {
			$('#category_selected_' + cid).remove();
			var tem_val = $('#coin_offer_categories_select_hidden').val();
			var tem_arr = tem_val.split(',');			
			var new_arr = new Array();
			for (var i=0; i < tem_arr.length; i++ ) {
				if ( tem_arr[i] != cid ) {
					new_arr.push(tem_arr[i]);
				} 
			}
			$('#coin_offer_categories_select_hidden').val( new_arr.toString() );
			$('#link_select_categories').show();
		}
			
}

function initialisePageOnLoad() {

    if ( document.getElementById('tbl_gallery1') ) {
        document.getElementById('tbl_gallery1').style.visibility = 'visible';
    }
    
    if ( document.getElementById('tbl_gallery2') ) {
        document.getElementById('tbl_gallery2').style.visibility = 'visible';
    }
    
    if ( document.getElementById('product_photos_present') ) {
        document.getElementById('product_photos_present').style.visibility = 'visible';
    }    
        
}


function limitChars(textid, limit, infodiv) {

    var text = $('#'+textid).val();    
    var textlength = text.length;
    
    if( textlength > limit ) {
        alert(_jt("Error: you have reached the character limit! Maximum characters allowed:") + ' ' + limit);
        $('#'+textid).val(text.substr(0,limit));
        $('#' + infodiv).html(_jt("Remaining characters: 0"));
        return false;
    } else {
        $('#' + infodiv).html(_jt("Remaining characters:") + ' <b>' + (limit - textlength) + '</b>');
        return true;
    }
    
}


function search_coins_toggle_methods(show_div, hide_div)
{

    document.getElementById(hide_div).style.display = 'none'; 
    document.getElementById(show_div).style.display = 'block';
    
    var f = document.form_search_coins;
    
    if (show_div == 'div-form-search-exact') {
        f.smode.value = 'search_exact';
    } else {
        f.smode.value = 'search_range';
    }
}



function myCoin_visibilityOffer(offer_id, cmdwhat)
{
    document.getElementById(offer_id).checked = true;
    $("#mark").val(cmdwhat);
    document.form_offers.submit();
}




function myCoins_markOffers(whatval)
{

    if (whatval == '') {
        return; 
 
    }

    var isAnyChecked = false;
    
    $('input:checkbox').each( 
        function() { 
           if (this.checked) {
                isAnyChecked = true;
           }
        } 
    );
    
    if ( isAnyChecked ) {
        document.form_offers.submit();
    } else {                
        alert(_jt("Please select at least one offer."));
        document.getElementById('mark').selectedIndex = 0;
        return;
    }


}

function checkboxesSelectGroup(whatval)
{
    if ( whatval == '' ) {
        return;
    }
    
    if ( whatval == 'all' ) {
        $('input:checkbox').attr('checked', 'checked');
    } else {
        $('input:checkbox').removeAttr('checked');
    }
}


function deal_accept_offer(coin_uniqid, offer_uniqid, offer_title) {

    //if ( confirm(_jt("Please confirm: \n\n would you like to give your coin to this offer:") + ' \n\n ' + offer_uniqid + ' \n\n ' + offer_title) ) {
        window.location.href = 'http://' + window.location.host + '/coin/deal_new_checkout.php?coin_uniqid=' + coin_uniqid + '&offer_uniqid=' + offer_uniqid;
    //}
}


function openCenteredWindow(url, name, width, height, params) {
   var left = Math.floor( (screen.width - width) / 2);
   var top = Math.floor( (screen.height - height) / 2);
   var winParams = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
   if (params) { winParams += "," + params; }
   var win = window.open(url, name, winParams);
   win.window.focus();
   return win;
}


function searchOffersSortByChangePage() {
	if ( $('#custom_search_sort_by').val() == '' ) {
		return;
	}

	var urlstring = decodeURI(document.location.href);

	//attach param at the end
	if ( urlstring.indexOf('?') > 0 ) {
		urlstring = urlstring + '&' + 'search_sort_by' + '=' + $('#custom_search_sort_by').val();
	} else {
		urlstring = urlstring + '?' + 'search_sort_by' + '=' + $('#custom_search_sort_by').val();
	}
	
	urlstring = urlstring + '&pageID=1'; //always start at 1st page...
	
		
	document.location.href = urlstring;   //todo uniq params... (pear page params does this on next page anyway...)
}


function urlChangeParam(urlstring, paramname, paramvalue) {
//todo...
// 	urlstring = decodeURI(urlstring);
// 
// 	//remove paramname from uri
// 	if ( urlstring.indexOf(paramname) > 0 ) {
//     	var parts = urlstring.split(paramname + '=');
// alert(parts.toString());    	
// 	}
// 
// 	//attach param at the end
// 	if ( urlstring.indexOf('?') > 0 ) {
// 		urlstring = urlstring + '&' + paramname + '=' + paramvalue;
// 	} else {
// 		urlstring = urlstring + '?' + paramname + '=' + paramvalue;
// 	}
// 	
// 	return urlstring;

}

