﻿
var ua = navigator.userAgent.toLowerCase();
var isOpera = ua.indexOf("opera") != -1; 
var isIE = ((ua.indexOf("msie") != -1) && !isOpera && (ua.indexOf("webtv") == -1)); 
var isGecko = ua.indexOf("gecko") != -1;
var isNetscape = ua.indexOf("netscape") != -1;
var isFirefox = ua.indexOf("firefox") != -1;

var DisplayPhotoFormat="0";
var DisplayPhotoFormat1="0";
var PhotoHaveFullFormat=0;
var PhotoHaveFormat1=0;
var DesplFormat=0;
var ImageWidth1=0, ImageHeight1=0, ImageWidth2=0, ImageHeight2=0, FullImageWidth=0, FullImageHeight=0, MainImageHeight=0;

function gebi(id) {return document.getElementById(id);}

function absPosition(obj) {
	this.x = 0;
	this.y = 0;
    while(obj) {
		this.x += obj.offsetLeft;
		this.y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return {x:this.x,y:this.y};
}
function defPosition(event) {
	if (document.attachEvent != null) {
		this.x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		this.y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (!document.attachEvent && document.addEventListener) {
		this.x = event.clientX + window.scrollX;
		this.y = event.clientY + window.scrollY;
	}
	return {x:this.x,y:this.y};
}
function countSubstrings(_str, _sub) {
	var count = 0;
	while (_str.indexOf(_sub) != -1) {
		_str = _str.substring(_str.indexOf(_sub) + _sub.length, _str.length);
		count++;
	}
	return count;
}

//垂直翻转
function flipvFilter() {
	if(isIE) {
		var cmd = document.forms["crop"].cmd;
		var steps = countSubstrings(cmd.value, "v") % 2;
		gebi("imgMPhoto").style.filter = (steps) ? "" : "FlipV";
		document.forms["crop"].cmd.value += "v";
		return false;
	} else return true;
}
//左右翻转
function fliphFilter() {
	if(isIE) {
		var cmd = document.forms["crop"].cmd;
		var steps = countSubstrings(cmd.value, "h") % 2;
		gebi("imgMPhoto").style.filter = (steps) ? "" : "FlipH";
		document.forms["crop"].cmd.value += "h";
		return false;
	} else return true;
}


var aOperations = new Array();
var bLoaded = false;
function resize_handler() {
	var imageToEdit = gebi("imgMPhoto");
	var divToMove = gebi("cropper");
	var divToMove1 = gebi("FilterDiv");
	
	var _x = 8;
	var _y = 8;
	divToMove.style.left = absPosition(imageToEdit).x - _x;
	divToMove.style.top = absPosition(imageToEdit).y - _y;
	divToMove.style.width = imageToEdit.offsetWidth + 2 * _x;
	divToMove.style.height = imageToEdit.offsetHeight + 2 * _y;
	
//	divToMove1.style.left = absPosition(imageToEdit).x;
//	divToMove1.style.top = absPosition(imageToEdit).y;
	divToMove1.style.width = imageToEdit.offsetWidth;
	divToMove1.style.height = imageToEdit.offsetHeight;
}

var Cropflag = false;
var Gridflag = false;
var resizeState = false;
var Filterflag = false;

function flipCrop() {
	    Cropflag = !Cropflag;
	    resize_handler();

	    if(Cropflag){
	        $('#cropper').show();
	    }else{
	        clearCrop();
	}
	Filterflag = false;
	$('#FilterDiv').hide();
	$('#FiltresProp').hide();
}
//复原
function clearCrop() {
	gebi("top").style.height = 8;
	gebi("bottom").style.height = 8;
	gebi("left").style.width = 8;
	gebi("right").style.width = 8;
	gebi("top_left").style.height = 8; gebi("top_left").style.width = 8;
	gebi("bottom_left").style.height = 8; gebi("bottom_left").style.width = 8;
	gebi("top_right").style.height = 8; gebi("top_right").style.width = 8;
	gebi("bottom_right").style.height = 8; gebi("bottom_right").style.width = 8;
	initMovingCenter(false);
	$('#cropper').hide();
}
//辅助线
function flipGrid() {
	Gridflag = !Gridflag;
	if(Gridflag) {
		if(!Cropflag) {
		    flipCrop();
		}
		$("#grid").show();
	} else {
		$("#grid").hide();
	}
}

function filtresact() {
	Cropflag = false;
	clearCrop();
	resize_handler();
    Filterflag = !Filterflag;
	if(Filterflag) {
	    $('#FilterDiv').show();
	    $('#FiltresProp').show();
	}
	else
	{
	    $('#FilterDiv').hide();
	    $('#FiltresProp').hide();
	};
	return false;
}

var CurrentSide = null;
var x0 = 0;
var y0 = 0;
function MouseDown(side, ev) {
	CurrentSide = side;
	ev = ev || window.event;
	x0 = defPosition(ev).x;
	y0 = defPosition(ev).y;
}
function MouseUp() {
	CurrentSide = null;
}
function initMovingCenter(flag) {
	resizeState = flag;
	gebi("mdlblck").style.cursor = (flag) ? "move" : "";
}
function MouseMove(ev) {
	if (CurrentSide == null) return;
	
	var mainPc = gebi("imgMPhoto");
	var ee = mainPc.offsetHeight;
	var tp = gebi("top");
	var bt = gebi("bottom");
	var md = gebi("mdlblck");

	ev = ev || window.event;
	dx = defPosition(ev).x - x0;
	dy = defPosition(ev).y - y0;
	
	var curObj = gebi(CurrentSide);
	if (CurrentSide != "mdlblck") initMovingCenter(true);
	try {
	switch (CurrentSide) {
		case "mdlblck":
			if (!resizeState) return;
			if (dy >= 0 && (gebi("top").offsetHeight + gebi("bottom").offsetHeight + 5 + dy) >= ee) return;
			if (dy < 0  && (gebi("top").offsetHeight + 5 + gebi("bottom").offsetHeight - dy) >= ee) return;
			if (gebi("top").offsetHeight + dy >= 8 && gebi("bottom").offsetHeight - dy >= 8) {
				gebi("top").style.height = gebi("top").offsetHeight + dy;
				gebi("bottom").style.height = gebi("bottom").offsetHeight - dy;
			}
			if (gebi("top").offsetHeight < 8) gebi("top").style.height = 8;
			else if (gebi("bottom").offsetHeight < 8) gebi("bottom").style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			if (gebi("left").offsetWidth + dx >= 8 && gebi("right").offsetWidth - dx >= 8) {
				gebi("left").style.width = gebi("left").offsetWidth + dx;
				gebi("right").style.width = gebi("right").offsetWidth - dx;
			}
			if (gebi("left").offsetWidth < 8) gebi("left").style.width = 8;
			else if (gebi("right").offsetWidth < 8) gebi("right").style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "top":
			if (dy >= 0 && (tp.offsetHeight + bt.offsetHeight + 5 + dy) >= ee) return;
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight + dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "bottom":
			if (dy < 0 && (tp.offsetHeight + 5 + bt.offsetHeight - dy) >=ee) return;
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight - dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "left":
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth + dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "right":
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth - dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "top_left":
			if (dy >= 0 && (tp.offsetHeight + bt.offsetHeight + 5 + dy) >= ee) return;
			curObj = gebi("top");
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight + dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			curObj = gebi("left");
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth + dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
		case "bottom_left":
			if (dy < 0 && (tp.offsetHeight + 5 + bt.offsetHeight -dy) >=ee) return;
			curObj = gebi("bottom");
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight - dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			curObj = gebi("left");
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth + dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
			
		case "top_right":
			if(dy >= 0 && (tp.offsetHeight + bt.offsetHeight + 5 + dy) >= ee) return;
			curObj = gebi("top");
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight + dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			curObj = gebi("right");
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth - dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;
			
		case "bottom_right":
			if (dy < 0 && (tp.offsetHeight + 5 + bt.offsetHeight -dy) >=ee) return;
			curObj = gebi("bottom");
			if (curObj.offsetHeight >= 8) curObj.style.height = curObj.offsetHeight - dy;
			if (curObj.offsetHeight < 8) curObj.style.height = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			curObj = gebi("right");
			if (curObj.offsetWidth >= 8) curObj.style.width = curObj.offsetWidth - dx;
			if (curObj.offsetWidth < 8) curObj.style.width = 8;
			else {
				x0 = defPosition(ev).x;
				y0 = defPosition(ev).y;
			}
			break;	
	} 
	} catch (all) {}
}
