/**
 *  Copyright © 2010 Digia Plc
 *  Copyright © 2010 Nokia Corporation
 *
 *  All rights reserved.
 *
 *  Nokia and Nokia Connecting People are registered trademarks of
 *  Nokia Corporation.
 *  Java and all Java-based marks are trademarks or registered
 *  trademarks of
 *  Sun Microsystems, Inc. Other product and company names
 *  mentioned herein may be
 *  trademarks or trade names of their respective owners.
 *
 *
 *  Subject to the conditions below, you may, without charge:
 *
 *  ·  Use, copy, modify and/or merge copies of this software and
 *     associated documentation files (the "Software")
 *
 *  ·  Publish, distribute, sub-licence and/or sell new software
 *     derived from or incorporating the Software.
 *
 *
 *  This file, unmodified, shall be included with all copies or
 *  substantial portions
 *  of the Software that are distributed in source code form.
 *
 *  The Software cannot constitute the primary value of any new
 *  software derived
 *  from or incorporating the Software.
 *
 *  Any person dealing with the Software shall not misrepresent
 *  the source of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 *  KIND, EXPRESS OR IMPLIED,
 *  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import Qt 4.7
import "../colibri"
import "styles"
import "javascript/sql.js" as DB

Rectangle {
    id: window

    property real camWidth : window.width*0.3
    property real camHeight : window.height*0.3
    property real scaleHeight : window.height/424
    property real scaleWidth : window.width/640
    property real scaleOverall : Math.min(scaleHeight,scaleWidth);
    property real scale : 0.25 * scaleOverall
    property real scaleValueBig : 2.5
    property CLStyle style : StyleHomeControlSystem {}
    property int selectedCam: -1
    property int lastZ: 5

    signal closeWindow()

    /**
     * Scales camera picture
     *
     * @param index The index of the camera
     * @return Nothing
     */
    function scaleCam(index) {
        if( index >= 0 ) {
            if( window.selectedCam == index ) {
                camGrid.children[index].width = Math.round(camGrid.width*0.48);
                camGrid.children[index].height = Math.round(camGrid.height*0.48);
                window.selectedCam = -1;
            } else {
                window.selectedCam = index;
                camGrid.children[index].z = window.lastZ + 1;
                camGrid.children[index].width = Math.round(camGrid.width*1);
                camGrid.children[index].height = Math.round(camGrid.height*1);
                window.lastZ = window.lastZ + 1;
            }
        }
    }

    /**
     * Sets alarm On/Off
     *
     * @return Nothing
     */
    function alarmClicked() {
        if(alarm.checked) {
            DB.setAlarmSystemOn(true);
        } else {
            DB.setAlarmSystemOn(false);
        }
    }

    width: 640
    height: 420
    color: "transparent"

    Timer {
        interval: 1000; running: true; repeat: true
        onTriggered: {
            if(DB.isAlarmSystemOn()) alarm.checked = true;
            else alarm.checked = false;
        }
    }


    Rectangle{
        width:window.width*0.955
        height:window.height*0.924
        color:style.selectionColor
        anchors.centerIn:parent
        radius:7
    }

    Image {
        source: "images/control_bg_trans_420x275_png.png";
        anchors.fill: parent;
        smooth: true
        opacity:0.7
    }

    RoomExitButton {
        windowWidth: window.width
        windowHeight: window.height
        style: window.style
        onClicked:  window.closeWindow();
    }

    Text {
        id: textSecurity
        text: "Security"
        font.pixelSize: 1 + Math.round( 23 * (window.height / 424) )
        x: Math.round( window.width * 0.05 )
        y: Math.round( window.height * 0.05 )
        color: window.style.textColor
        font.bold: true
     }

     Row {
        y: window.height*0.045
        anchors.right: parent.right
        anchors.rightMargin: window.width*0.12
        Row {
            Text {text: "Alarm System: "; font.pointSize: 0.001 + window.height*0.036; anchors.verticalCenter: parent.verticalCenter; color: window.style.textColor}
            CLCheckBox{id: alarm; checkMark: "On"; style:window.style; height: window.height*0.14; width: height; fontSize: window.height*0.06; onClicked: alarmClicked()}
        }
     }

     Rectangle {
            id: camGrid
            height: window.height*0.75
            width: window.width*0.75
            anchors.horizontalCenter: parent.horizontalCenter;
            y: window.height*0.20
            color: "transparent"

            Rectangle {
                border.color: style.textColor
                border.width: 2
                width: (selectedCam==0)?camGrid.width : camGrid.width*0.48; height: (selectedCam==0)? camGrid.height : camGrid.height*0.48
                anchors.left: camGrid.left; anchors.top: camGrid.top; z: (width!=camGrid.width*0.48)? 6 : 5
                Image {
                    id: img1
                    source:"images/camera1.jpg"
                    width: parent.width - parent.border.width; height: parent.height - parent.border.width
                    x: parent.border.width/2; y: parent.border.width/2
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        if(selectedCam == 0) {
                            selectedCam = -1;
                        } else if(selectedCam == -1) {
                            selectedCam = 0;
                        }
                    }
                }
                Behavior on width { PropertyAnimation { duration: 100; } }
                Behavior on height { PropertyAnimation { duration: 100; } }
            }

            Rectangle {
                border.color: style.textColor
                border.width: 2
                width: (selectedCam==1)?camGrid.width : camGrid.width*0.48; height: (selectedCam==1)? camGrid.height : camGrid.height*0.48
                anchors.right: camGrid.right; anchors.top: camGrid.top; z: (width!=camGrid.width*0.48)? 6 : 5
                Image {
                    id: img2
                    source:"images/camera2.jpg"
                    width: parent.width - parent.border.width; height: parent.height - parent.border.width
                    x: parent.border.width/2; y: parent.border.width/2
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        if(selectedCam == 1) {
                            selectedCam = -1;
                        } else if(selectedCam == -1) {
                            selectedCam = 1;
                        }
                    }
                }
                Behavior on width { PropertyAnimation { duration: 100; } }
                Behavior on height { PropertyAnimation { duration: 100; } }
            }

            Rectangle {
                border.color: style.textColor
                border.width: 2
                width: (selectedCam==2)? camGrid.width : camGrid.width*0.48; height: (selectedCam==2)? camGrid.height : camGrid.height*0.48
                anchors.left: camGrid.left; anchors.bottom: camGrid.bottom; z: (width!=camGrid.width*0.48)? 6 : 5
                Image {
                    id: img3
                    source:"images/camera4.jpg"
                    width: parent.width - parent.border.width; height: parent.height - parent.border.width
                    x: parent.border.width/2; y: parent.border.width/2
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        if(selectedCam == 2) {
                            selectedCam = -1;
                        } else if(selectedCam == -1) {
                            selectedCam = 2;
                        }
                    }
                }
                Behavior on width { PropertyAnimation { duration: 100; } }
                Behavior on height { PropertyAnimation { duration: 100; } }
            }

            Rectangle {
                border.color: style.textColor
                border.width: 2
                width: (selectedCam==3)? camGrid.width : camGrid.width*0.48; height: (selectedCam==3)? camGrid.height : camGrid.height*0.48
                anchors.right: camGrid.right; anchors.bottom: camGrid.bottom; z: (width!=camGrid.width*0.48)? 6 : 5
                Image {
                    id: img4
                    source:"images/camera3.jpg"
                    width: parent.width - parent.border.width; height: parent.height - parent.border.width
                    x: parent.border.width/2; y: parent.border.width/2
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        if(selectedCam == 3) {
                            selectedCam = -1;
                        } else if(selectedCam == -1) {
                            selectedCam = 3;
                        }
                    }
                }
                Behavior on width { PropertyAnimation { duration: 100; } }
                Behavior on height { PropertyAnimation { duration: 100; } }
            }
     }
}
