var s_url_base="http://tester/Smirnov/Sticky/" ;
var _curr_image=0 ; //keeps current image id

function newWindow(url){
window.open(url,"Window1","menubar=no,width=600,height=600,toolbar=no");
}

function clearNoBrandRadio(){
	r_radio = document.getElementsByName("price_no_brand") ;
	if(r_radio)
		for(i=0; i<r_radio.length;i++)
			r_radio[i].checked=false ;
}
function clearBrandedRadio(){
	r_radio = document.getElementsByName("price_branded") ;
	if(r_radio)
		for(i=0; i<r_radio.length;i++)
			r_radio[i].checked=false ;
}

/*
 *
 */
function hideEnlargeTemplate(){
	_div = document.getElementById("template-enlarge") ;//template-enlarge-image
	_image = document.getElementById("template-enlarge-image") ;//template-enlarge-image
	_span = document.getElementById("template-enlarge-span") ;//template-enlarge-image
	if(_div!=null)_div.style.display = "none" ;
}

function get_client_size() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}

function get_scroll_pos() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ){
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
    	scrOfY = document.body.scrollTop;
    	scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

/*
 *
 */
function enlargeTemplate(_id_){
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	hideEnlargeTemplate() ;
	_div = document.getElementById("template-enlarge") ;
	_wind = get_client_size() ;
	_scroll = get_scroll_pos() ;
	
	if(_div!=null){
		var _img = new Image() ;
		_curr_image = _id_ ;
		_img.id="imgId"+_id_ ;
		_img.onload=function(){
			if(("imgId"+_curr_image)==this.id){
				_image = document.getElementById("template-enlarge-image") ;
				_span= document.getElementById("template-enlarge-span") ;
				_image.style.display="none" ;
				_span.style.display="none" ;
				_div_width = 0;
				_div_height = 0;
				if(_image!=null){
					if ((version >= 5.5 && version < 7.0) && (document.body.filters)){
						_span.style.width=0;
						_span.style.height=0;
						_span.style.filter="" ;
						//_image.style.width= this.width+"px";
						//_image.style.height = this.height+"px" ;
						_span.style.width= this.width+"px";
						_span.style.height = this.height+"px" ;
						_span.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'"+this.src+"',sizingMethod='scale')" ;
						_span.style.display="block" ;
					}else{
						_image.src=this.src ;
						_image.style.width= this.width+"px";
						_image.style.height = this.height+"px" ;
						_image.style.display="block" ;
					}
				
					_div_width = this.width;
					_div_height = this.height;
					
					_div.style.left=0;
					_div.style.top=0;
					
					//_div.style.width= this.width+"px";
					//_div.style.height = this.height+"px" ;
					_div.style.left=(_scroll[0]+((_wind[0]/2)-(_div_width/2)))+"px" ;
					_div.style.top =(_scroll[1]+((_wind[1]/2)-(_div_height/2)))+"px" ;
					_div.style.display="inline" ;
				}
			}
		}
		//_img.src="sticky.php?request=template_image&id="+_id_ ;
		_img.src="sticky.php?request=preview&id="+_id_ ;
	}
}

/*
 *
 */
function fixPNGs(){
	var arVersion = navigator.appVersion.split("MSIE") ;
	var version = parseFloat(arVersion[1]) ;
	if ((version >= 5.5) && (document.body.filters)){
		for(var i=0; i<document.images.length; i++){
			var img = document.images[i] ;
			var imgName = img.src.toUpperCase() ;
			if(imgName.substring(imgName.length-3, imgName.length) == "PNG" || imgName.substring(imgName.length-3, imgName.length) == "png"){
				var imgID = (img.id) ? "id='" + img.id + "' " : "" ;
				var imgClass = (img.className) ? "class='" + img.className + "' " : "" ;
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' " ;
				var imgStyle = "display:inline-block;" + img.style.cssText  ;
				if (img.align == "left") imgStyle = "float:left;" + imgStyle ;
				if (img.align == "right") imgStyle = "float:right;" + imgStyle ;
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle ;
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
								+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
								+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
								+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" ;
				img.outerHTML = strNewHTML ;
				i = i-1 ;
			}
		}
	}
}
/*
 *
 */
function previewSticky(){
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);

	var _id=document.getElementById("stickyId").value ;
	var _templateId=document.getElementById("templateId").value ;
		
	if ((version >= 5.5 && version < 7.0) && (document.body.filters)){
		var tempImg = new Image() ;
		tempImg.onload = function(){
			var _span = document.getElementById("sticky-preview") ;
			_span.style.width= this.width;
			_span.style.height = this.height ;
			_span.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'"+this.src+"',sizingMethod='scale')" ;
		}
		tempImg.src=s_url_base+"sticky.php?request=preview&id="+_id+"&templateId="+_templateId ;
	}else{
		document.getElementById("sticky-preview").src=s_url_base+"sticky.php?request=preview&id="+_id+"&templateId="+_templateId ;
	}
}

/**
 * 
 */
function validateStartEndDates(){
	var startDate = document.getElementById("id_form_startDate").value ;
	var endDate = document.getElementById("id_form_endDate").value ;
	if(startDate!=null && endDate!=null){
		if(startDate.split('-').join('')<=endDate.split('-').join('')) return true;
	}
	return false ;
}

/**
 * 
 */
function saveSticky(){
	if(validateStartEndDates()==false){
		//if(!confirm("Invalid values for starting/ending dates.\nStarting data cannot be greater than ending date.\nClick 'OK' to save sticky anyway, or 'Cancel' to change the dates.")) return ;
		alert("'Show From' Date cannot be greater than 'Show To' Date") ;
	}else document.getElementById("ModifyStickyForm").submit() ;
}

/**
 * 
 */
function deleteSticky(){
	if(confirm('Are you sure you want to DELETE this Sticky?')){
		document.getElementById("request").value="DeleteSticky" ;
		document.getElementById("ModifyStickyForm").submit() ;
	}
}
 
/**
 *
 */
function showTemplatePicker(){
	var picker = document.getElementById("template-picker") ;
	//alert(getStyle(picker,"height")) ;
	picker.style.top = (findPos(document.getElementById("btn_templatePicker"))[1]-getStyle(picker,"height")-5)+"px" ;
	//alert(document.body.clientWidth);
	picker.style.left = ((document.documentElement.clientWidth-getStyle(picker,"width"))/2)+"px" ;
	picker.style.visibility = 'visible' ;
}

/**
 *
 */
function hideTemplatePicker(){document.getElementById("template-picker").style.visibility = "hidden";}

/**
 *
 */
function setTemplate(_id){
	document.getElementById("templateId").value = _id;
	hideTemplatePicker() ;
}

/**
 *
 */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

/**
 *
 */
function getStyle(el, style) {
	if(!document.getElementById) return;
   
	var value = el.style[toCamelCase(style)];
   
	if(!value)if(document.defaultView)value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
	else if(el.currentStyle) value = el.currentStyle[toCamelCase(style)];
     value = value.replace(/px/," ") ;
     return value;
}

/** toCamelCase(input)
 * Converts string input to a camel cased version of itself.
 * For example:
 * toCamelCase("z-index"); // returns zIndex
 * toCamelCase("border-bottom-style"); // returns borderBottomStyle.
 */
function toCamelCase(s) {
	for(var exp = toCamelCase.exp; 
		exp.test(s); s = s.replace(exp, RegExp.$1.toUpperCase()) );
	return s;
}
toCamelCase.exp = /-([a-z])/;